Nginx, nuxt.js static generated mode and redirection of trailing slashs

Viewed 693

i currently have a problem configuring my Nginx correctly for nuxt.js generated sites. What i want to achieve is the following

/magazin -> /magazin/index.html
/magazin/ -> 301 /magazin

/magazin/artikel/titel-goes-here -> /magazin/artikel/titel-goes-here/index.html
/magazin/artikel/titel-goes-here/ -> 301 /magazin/artikel/titel-goes-here

currently this is the other way around.

If im correct i shouldn't use a proxy pass to a e.g. pm2 instance with express etc. as it destroys the sense of static site generation.

But how can i get this page structure to work, as i need the same url's as our legacy service for SEO reasons, which used Angular Universal SSR

my current config is:

location ^~ /magazin {
   root /path/to/dist;
   index index.html ;
}

if i add something like

rewrite ^(.+)/+$ $1 permanent; 

i get an infinite 301 loop

Thanks for the help

1 Answers

You cannot use the built in index directive, as it works the other way around (as you have observed).

You can use try_files to test for the existence of the index.html file. Use a named location to process the redirection.

For example:

location ^~ /magazin {
    root /path/to/dist;
    try_files $uri $uri/index.html @rewrite;
}
location @rewrite {
    rewrite ^(.+)/$ $1 permanent;
}

See this document for details.

Related