Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am testing nginx and want to output variables to the log files. How can I do that and which log file will it go (access or error).

share|improve this question
up vote 8 down vote accepted

You can set a custom access log format using the log_format directive which logs the variables you're interested in.

share|improve this answer
thanks, and I guess there is no easier way to output one variable by itself? – lulalala Jul 4 '12 at 5:39
@lulalala Not that I know of. – mgorven Jul 4 '12 at 5:42

If you're in a development environment, you can send variables via headers.

add_header X-uri "$uri";

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


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 will trigger the latter header while visiting will trigger the former header.

share|improve this answer
Note that add_header will work on successful requests only. Documentation states that it can only be applied to responses with codes 200, 204, 301, 302 or 304. Therefore, it can't be used to debug HTTP errors. – John WH Smith Aug 25 '14 at 14:43
Ah, this is genius! Thanks! – L0j1k Apr 24 '15 at 6:28
@JohnWHSmith : As marat noted in this answer. As of version 1.7.5, nginx added an "always" parameter to add_header which will return the header, no matter what the response code. So for example, add_header X-debug-message "A php file was used" always;, should work even for 500 error code. – yuvilio Sep 22 '15 at 20:24

You can return a simple string as HTTP response:

location /
    return 200 $document_root;
share|improve this answer

Another option is to include the echo module when you build nginx, or install OpenResty which is nginx bundled with a bunch of extensions (like echo.)

Then you can simply sprinkle your configuration with statements like:

echo "args: $args"
share|improve this answer
When I try this it echos out to a plain text file on the server interrupting the output of the actual page. – JaredMcAteer May 7 '15 at 17:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.