Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make this try_files directive work?

Tags:

nginx

I am trying to understand how Nginx's try_files directive works. Nginx is running on my server and serving up the default page located at /usr/share/nginx/html/index.html.

However, I have a simple HTML page located in the filesystem at /var/www/test/index.html. The following config file is not causing that file to get served. I'd like to understand why not, and what change I need to make so that it will be served.

Here is the relevant portion of default.conf:

server {     listen       80;     server_name  localhost;      root  /var/www;     try_files /test/index.html /;      location / {         root   /usr/share/nginx/html;         index  index.html index.htm;     } 
like image 955
Jonah Avatar asked Jul 22 '13 22:07

Jonah


People also ask

How try_files work Nginx?

NOTE: Nginx try_files directive recursively searches for files and directories specified from left to right until it finds ones. Specifying this directive in the location / can cause performance issues, especially on sites with massive traffic. Therefore, you should explicitly specify the location block for try_files.

How does Nginx location work?

NGINX location matching exact URLNGINX always tries to match most specific prefix location at first. Therefore, the equal sign in the following location block forces an exact match with the path requested and then stops searching for any more matches.

What is Nginx directive?

Configuration options in Nginx are known as directives. This option has name and parameters, and this must end with a semicolon (;) otherwise Nginx will fail to load the configuration and produce an error.

What is Nginx document root?

On Ubuntu, the Nginx web server stores its documents in /var/www/html , which is typically located on the root filesystem with the rest of the operating system. Sometimes, though, it's helpful to move the document root to another location, such as a separate mounted filesystem.


1 Answers

a very common try_files line which can be applied on your condition is

location / {     try_files $uri $uri/ /test/index.html; } 

you probably understand the first part, location / matches all locations, unless it's matched by a more specific location, like location /test for example

The second part ( the try_files ) means when you receive a URI that's matched by this block try $uri first, for example http://example.com/images/image.jpg nginx will try to check if there's a file inside /images called image.jpg if found it will serve it first.

Second condition is $uri/ which means if you didn't find the first condition $uri try the URI as a directory, for example http://example.com/images/, ngixn will first check if a file called images exists then it wont find it, then goes to second check $uri/ and see if there's a directory called images exists then it will try serving it.

Side note: if you don't have autoindex on you'll probably get a 403 forbidden error, because directory listing is forbidden by default.

EDIT: I forgot to mention that if you have index defined, nginx will try to check if the index exists inside this folder before trying directory listing.

Third condition /test/index.html is considered a fall back option, (you need to use at least 2 options, one and a fall back), you can use as much as you can (never read of a constriction before), nginx will look for the file index.html inside the folder test and serve it if it exists.

If the third condition fails too, then nginx will serve the 404 error page.

Also there's something called named locations, like this

location @error { } 

You can call it with try_files like this

try_files $uri $uri/ @error; 

TIP: If you only have 1 condition you want to serve, like for example inside folder images you only want to either serve the image or go to 404 error, you can write a line like this

location /images {     try_files $uri =404; } 

which means either serve the file or serve a 404 error, you can't use only $uri by it self without =404 because you need to have a fallback option.
You can also choose which ever error code you want, like for example:

location /images {     try_files $uri =403; } 

This will show a forbidden error if the image doesn't exist, or if you use 500 it will show server error, etc ..

like image 107
Mohammad AbuShady Avatar answered Sep 22 '22 07:09

Mohammad AbuShady