2

I want to create an alias to the "static" folder:

location ~ ^/myapp/([a-zA-Z0-9_-]+)/ {
    alias /var/lib/myapp/$1/static/;
    autoindex on;
}

But, if I have the URL:

https://mydomain/myapp/section1/page.html

I'm being redirected to:

https://mydomain/myapp/section1/page.html/

which causes a 404.

If I access:

https://mydomain/myapp/section1/

I can correctly see the list of all the html files (because of "autoindex on").

However, if I have this config:

location /myapp/ {
    alias /var/lib/myapp/;
    autoindex on;
}

nginx does NOT add the trailing slash, and therefore I can correctly access the .html pages. The problem with this config is that "static/" has to be included in the URL, such as:

https://mydomain/myapp/section1/static/page.html

How can I make nginx NOT add a trailing slash in the first example above?

1

An alias within a regular expression location block requires a value which specifies the full path to the target file. You need to capture the remainder of the URI in the location statement and append it to the end of the alias statement.

For example:

location ~ ^/myapp/([a-zA-Z0-9_-]+)/(.*)$ {
    alias /var/lib/myapp/$1/static/$2;
    autoindex on;
}

See this document for details.

3
  • Thank you very much. I tried that before (and now again), but unfortunately that gives me 404 for every attempt, even with "autoindex on". I tried with and without the ".html" file in the URL. – Nuno Jan 5 '20 at 14:37
  • Have you cleared the browser’s cache. Also, are there any other location blocks above this one? – Richard Smith Jan 5 '20 at 15:32
  • DevTools open with "Disable Cache" enabled - any changes are immediate. The other location blocks are: "location = /", "location /" and "location /awstats/icon". – Nuno Jan 5 '20 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.