Search code examples
nginxwebserverhttpserver

Serving static HTML files in Nginx without extension in url


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


Solution

  • Try this

    location ~ ^/test/(.*)$ {
        alias /srv/myproject/xyz/main/;
        try_files $1.html =404;
    }