How to Serve "out" (next export) folder Nextjs with Nginx

Viewed 398

I am currently learning to deploy applications made with NextJs to VPS. I have been successful, running some REST APIs on the Nginx server, I am not using the NextJs api feature, this is separate using Express. This is executed using PM2.

But I am confused, how do I serve NextJs "out" the results folder "next build && next export", this is a dashboard page that fetches data on the client side.

Do I have to treat the same with the REST API using PM2 or a different treatment, can you please provide an example configuration file of nginx for this.

I have tried googling but there is no exact answer about this.

Thanks in advance, I appreciate any answer.

1 Answers
server {
    server_name  your-website.com;
  
    location / {
        root   /app; # name of the folder where you put content of out directory, try files will be relative to this path
        try_files  $uri $uri.html $uri/ =404; # try different strategies to find matching file in the folder with build, otherwise throw 404 error
    }
  
    error_page 404 /404.html; # if 404, serve this file
}
Related