Serving static HTML files in Nginx without extension in url
Asked Answered
M

1

6

root directory = /srv/myproject/xyz/main/

in the "main" folder I have few *.html files and I want all of them to point at a url say /test/ (which is quite different from the directory structure)

this is my very basic nginx configuration

server {
    listen                80;
    error_log   /var/log/testc.error.log;

    location /test/ {
         root   /srv/myproject/xyz/main/;
         #alias /srv/myproject/xyz/main/;
         default_type   "text/html";
         try_files  $uri.html ;
    }
}

If I use simple alias

location /test/ {
       alias /srv/myproject/xyz/main/;   
}

then its work perfectly, I mean I can access those html files by http://www.myurl.com/test/firstfile.html and so on

but I dont want that html extension.

I tried to follow these threads but no success http://forum.nginx.org/read.php?11,201491,201494

How to remove both .php and .html extensions from url using NGINX?

how to serve html files in nginx without showing the extension in this alias setup

Moon answered 27/1, 2015 at 16:27 Comment(0)
S
6

Try this

location ~ ^/test/(.*)$ {
    alias /srv/myproject/xyz/main/;
    try_files $1.html =404;
}
Sopher answered 28/1, 2015 at 10:52 Comment(1)
What about URLs with trailing slash?Gobert

© 2022 - 2024 — McMap. All rights reserved.