How to Serve HTML Files in Nginx Without Showing The Extension in This Alias Setup

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

Apparently alias and try_files don't work together. However, I don't think you need to use alias.

location /mr {
default_type "text/html";
try_files /fullpath/$uri /fullpath/$uri.html /fullpath/$uri/index.html /fullpath/index.html;
}

Which would try:

  • Exact file.
  • File with .html added.
  • Index in the path.
  • Default index.

I think the root directive does work with try files but am unable to test.

server{
location /mr {

root /home/mysite/fullpath;

default_type "text/html";
try_files $uri $uri.html $uri/index.html index.html;
}
}

Serving static HTML files in Nginx without extension in url

Try this

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

Setup nginx to serve static html web pages based on url

You need to make use of the alias directive instead of root: see documentation.

server {

root /;

location /desktop {
}

location /mobile {
alias /mobile;
}
}

(don't forget trailing semicolons)

nginx redirects all files with or without extensions except a few

Solved I auth.php was not included as an exception which would cause a loop.

location ~ .+(?<!/|auth.php|.txt|.pdf|.jpeg|.jpg|.png)$ { 

auth_request /auth.php;
error_page 401 = @login;

}

Nginx location to index certain html file

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

Nginx as reverse-proxy: serving files without extension

The problem is, that you need something to identify the files without any extension. A sub-directory or something that’s always present within the request. Your regular expression only matched for requests that start end end with a dot (e.g. http://example.com/.). The following server configuration assumes that all URLs start with storage, as this would be the only possibility to identify those files.

Please note that I’m using the try_files directive to rewrite the internal path where nginx should look for the file. The root directive is not meant for what you want to achieve.

And last but not least, you should always nest location blocks with regular expressions. There is no limit in the nesting level. nginx will create some kind of tree data structure to search for the best matching location. So think of a tree while writing the blocks.

server {
listen 80 default;
listen [::]:80 default ipv6only=on;
server_name www.website.com website.com;
root /home/website/public_html/app;

location / {

# Matches any request for a URL ending on any of the extension
# listed in the regular expression.
location ~* \.(jpe?g|gif|css|png|js|ico|txt|xml)$ {
expires 30d
access_log off;
try_files /public$uri =404;
}

# Matches any request starting with storage.
location ~* ^/storage {
access_log /var/log/nginx/bt.views.access.log;
error_log /var/log/nginx/bt.views.error.log;
try_files /app$uri;
}

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
access_log off;
}
}


Related Topics



Leave a reply



Submit