How to output variable in nginx log for debugging

You can send nginx variable values via headers. Handy for development.

add_header X-uri "$uri";

and you’ll see in your browser’s response headers:

X-uri:/index.php

I sometimes do this during local development.

It’s also handy for telling you if a subsection is getting executed or not. Just sprinkle it inside your clauses to see if they’re getting used.

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
    add_header X-debug-message "A static file was served" always;
    ...
}

location ~ \.php$ {
    add_header X-debug-message "A php file was used" always;
    ...
}

So visiting a url like http://www.example.com/index.php will trigger the latter header while visiting http://www.example.com/img/my-ducky.png will trigger the former header.

Leave a Comment