Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: try_files outside location

I'm configuring a pretty standard webserver using nginx. The server is working as expected, however, there is a small configuration detail I would like to understand.

My current configuration is:

index index.html index.htm index.php;

location / {
    try_files $uri $uri/ /index.php?q=$uri;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}

With this configuration, if I access to: http://myweb.com/wp-content/uploads/2012/10/cropped-bitmap11.png/lol.php I get a 404 as expected.

However, with this configuration:

try_files $uri =404;

location ~ \.php$ {
    fastcgi_index index.php;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}

I get a blank page with "Access denied".

Why is the result different?

Thank you

like image 822
Sergio Lopez Avatar asked Oct 30 '12 11:10

Sergio Lopez


1 Answers

You are probably under the impression that try_files on server level must work for every request. Not at all. Quite the contrary, it works only for requests that match no location blocks.

like image 84
VBart Avatar answered Nov 15 '22 04:11

VBart