Debugging Nginx Configuration Trick
Today I had an issue where I was trying to debug a problem with an nginx configuration, I came up with a simple trick. One of the hardest parts of nginx configurations, especially with rewrites, is you might not know which “location” directive is not working as expected.
In PHP, sometimes you would just add something like this:
echo "I'm here!"; exit();
However, in Nginx configuration files, it isn’t as easy…
… or is it?
One thing that works well is the rewrite directive. You can append variables to the URL to be rewritten. Another great thing is a rewrite statement can go just about anywhere. So lets say we were trying to debug this location statement:
location ~ /api/.*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/www/$fastcgi_script_name;
}
Now lets say its returning a 404, and I’m not 100% sure what the actual value of $fastcgi_script_name is. I can add this to it:
location ~ /api/.*\.php$ {
## ADD HERE
redirect ^ http://www.google.com/?q=$fastcgi_script_name last; break;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/www/$fastcgi_script_name;
}
This will redirect your HTTP request to Google.com and put the value in the query textfield. Bingo, I can easily see the actual value! Pretty helpful when you have a large, complex server definition.






