Home > database >  How to deploy Next.js static export with Nginx? (deep links not working)
How to deploy Next.js static export with Nginx? (deep links not working)

Time:05-28

I made a next.js export into the out folder.

Folder structure is:

  • out
    • index.html
    • terms.html
    • privacy.html

I set up nginx to serve files from this folder:

server {
    root /var/www/myproject/out;
    index index.html index.htm index.nginx-debian.html;

    server_name myproject.com;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

The main page (index) opens fine. Navigation from within the app to urls like myproject.com/privacy works fine. The problem is if I try to open these links directly, it will serve the main page (index) instead of the actual pages, since those urls don't exist in the folder. The only way to open the privacy page directly is adding the html extension to the url: myproject.com/privacy.html.

How to configure nginx to serve the actual page myproject.com/privacy.html when someone enters the myproject.com/privacy url?

CodePudding user response:

Issue is in try_files.

As current configuration includes:

  • / (which default route to index.html at root path)
  • index.html
  • /index.html
  • test/*.html

To access pages route path name without extension i.e., /privacy that format should be included in try_files insdie location /

Try this:

try_files $uri $uri.html /$uri /index.html
  • Related