Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 907
micheal Avatar asked Jan 27 '15 16:01

micheal


People also ask

How do I serve static content in nginx?

To serve static files with nginx, you should configure the path of your application's root directory and reference the HTML entry point as the index file. In this example, the root directory for the snake deployment is /home/futurestudio/apps/snake which contains all the files.

Where do I put html files in nginx?

By default Nginx Web server default location is at /usr/share/nginx/html which is located on the default file system of the Linux.

How do you serve a static file?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.


1 Answers

Try this

location ~ ^/test/(.*)$ {
    alias /srv/myproject/xyz/main/;
    try_files $1.html =404;
}
like image 73
SuddenHead Avatar answered Sep 20 '22 23:09

SuddenHead