Which nginx location stanza is being evaluated?

One thing I’ve had to get used to working with nginx, is that it can be hard to understand which configuration stanza (which part / section of the nginx virtualhost configuration file) is being evaluated, especially given an existing site with a complex history.

Useful trick here is to add a custom header for each of the sections, like so:

server {
add_header X-My-Debug-Header-01 srv;
listen 80 default_server;

location ~ test\.php$ {
add_header X-My-Debug-Header-02 loc-test-php;
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}

location /pictures {
add_header X-My-Debug-Header-03 loc-pictures;
alias /usr/share/nginx/html;
index index.php index.html index.htm;
}

}

Now, for each section match, there will be a convenient header which can be viewed with e.g. wget --server-response (wget -S) or curl --head (curl -I).

Caveats: the Firefox LiveHTTPHeaders plugin doesn’t seem to show non standard headers. Also, when nginx serves a 40x or 50x, the custom header tends not to get served.

Leave a Reply