Troubleshooting Techniques
[ 204 ]
Using if instead of try_files
One such case is a situation in which a user wants to deliver a static file if it is found
on the filesystem, and if not, to pass the request on to a FastCGI server:
server {
root /var/www/html;
location / {
if (!-f $request_filename) {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
break;
}
}
}
This was the way this problem was commonly solved before NGINX had the try_
files directive, which appeared in Version 0.7.27. The reason why this is considered a
configuration error is that it involves using if within a location directive. As detailed
in the Converting an "if"-fy configuration to a more modern interpretation section in Chapter
4 , NGINX as a Reverse Proxy, this can lead to unexpected results or possibly even a
crash. The way to correctly solve this problem is as follows:
server {
root /var/www/html;
location / {
try_files $uri $uri/ @fastcgi;
}
location @fastcgi {