I am new to nginx, I have read some of the documentation at wiki.nginx.org, as long as I know the try_files sentence will do exactly as it says, it will search a file in the order given, as I see this sentence is used extensively along with the $uri variable (which has the requested uri), however, nginx is not accessing the directories without the trailing slash even using the try_files sentence as this:

try_files $uri $uri/ =404;

if I am understanding well, if I look for this:, it will not find anything at the begining but it should find the directory at second (unless the index file is not being checked), but that is not happening, looking for a solution for this I have found this quetion add-slash-to-the-end-of-every-url-need-rewrite-rule-for-nginx, one of the answers suggested to apply this rewrite sentence in order to make the directories be accessed without a trailing slash (something that it worked):

rewrite ^([^.]*[^/])$ $1/ permanent;

my question is: is not the try_files sentence doing that already? (it has sense to me but it does not work as I think, I checked the access.log file in order to find a logic answer but I could not figure out this).

  • hopefully none of the meaning was lost by simplifying the question title. – Sirex May 24 '13 at 1:26

The $uri/ in try_files does not exactly add a / to the end of the URL and try it. Instead, when the parameter ends with a / the request is passed on to an index module, depending on whether you have specified the index, autoindex or random_index directives in your server or location blocks.

Thus, if you had specified:

index index.php;


try_files $uri $uri/ =404;

will look for index.php in that corresponding directory, when given a URL ending in /. If you used autoindex instead, then nginx would generate a directory listing for that directory.

  • I think I will choose your answer as the correct one because it seems to be logic, however, I insist the trailing slashes are used only to separate directories in a file path so this "/dir" and this "/dir/" should be the same, I will investigate more about this later anyway, I just have one final question, is that a good practice, isn't that a duplicate configuration? – MARP May 24 '13 at 13:19

"is the try_files sentence not doing that already?"

No. There is a gotcha with try_files:

In the event that no file is found, an internal redirect to the last parameter is invoked. Do note that only the last parameter causes an internal redirect, former ones just sets the internal URI pointer.

Nginx doesn't attempt to use the last parameter and check to see if it exists as a file or directory, instead it rewrites the request to match the last parameter, and then reprocesses the server block.

Without seeing the rest of your config file it is not easy to see the exact effect of this, but it has caused me issues in the past. You may be able to make a simple work around by doing:

try_files $uri $uri/ /404_static.html =404;

and also include another location block.

location = /404_static.html {
    root   /documents/projects/intahwebz/intahwebz/data/html/;

If you're passing nginx to a proxy, this does require a small work around to pass the correct URI to the backend e.g.:

location  / {
    set $originalURI  $uri;
    try_files $uri /routing.php /50x_static.html;
    fastcgi_param  QUERY_STRING  q=$originalURI&$query_string;

    fastcgi_pass   unix:/opt/local/var/run/php54/php-fpm-www.sock;
    include       /documents/projects/intahwebz/intahwebz/conf/fastcgi.conf;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.