Home > Back-end >  Nginx location to index certain html file
Nginx location to index certain html file

Time:10-31

// conf
server {
    listen 80;

    location /x {
        root /templates;
        index x.html;
    }
     location / {
        root /templates;
        index index.html;
    }
}
//
// Folder 
tempalets
| - index.html
| - x.html

I go to url domain.com, it's will show index.html

But, I go to url domain.com/x, it's will show 404 Not Found.

And, I try domain.com/x.html, it's will show x.html.

Why url domain.com/x doen't show x.html?

How could I go to url domain.com/x and show x.html?

I don't want that .html in the url.

CodePudding user response:

Alternative 1:

Use try_files. Below it concatenates the extension (.html), so when requesting /x it first checks if the file /templates/x.html exists, then /templates/x, otherwise it 404s.

server {
    listen 80;

    location / {
        root /templates;
        try_files $uri.html $uri =404;
        index index.html;
    }
}

Alternative 2:

Upload the HTML files without the extension and set the default_type (MIME) to text/html.

default_type 'text/html';

https://blog.uidrafter.com/pretty-routes-for-static-html

  • Related