Nginx – Serving static files in nginx using alias

nginx

I have got the following location specified in nginx:

location ~ ^/blah/(.*)$ {
   alias /html/$1;
   autoindex on;
   allow all;
}

The html directory has the following structure:

/html/
   - other/
       - test2.htm
   - test.htm

I can browse to http://server/blah and get a file listing.

I can serve http://server/blah/other/test2.htm.

When I try to access http://server/blah/test.htm I get a 301 and a redirect to http://server/blah/test.htm/ and then a 404.

How can I serve the page at http://server/blah/test.htm?

Best Answer

Probably redirect happens somewhere else in the config, adding break should help to get what you need:

location ~ ^/blah/(.*)$ {
    alias /full/path/to_your/html/dir/$1;
    autoindex on;
    fancyindex on;
    allow all;
    break;
}