83

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).

143

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.

  • 29
    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
  • 31
    @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
  • 6
    This doesn't answer the question at all. The guy wants to log to log file not to the client. – Avamander Apr 17 '18 at 11:31
37

You can return a simple string as HTTP response:

location /
{
    return 200 $document_root;
}
  • 1
    What if you want to return two variables values? – BringBackCommodore64 Mar 16 '18 at 16:41
  • Going to the location immediately opens the Save As dialog of my browser (tested on Opera, Chromium). No response whatsoever. – BringBackCommodore64 Mar 16 '18 at 17:27
  • @BringBackCommodore64 Adding a text/html Content-Type header may help. – bitwise May 9 at 20:26
  • Using Postman, this works without any additional headers. Thanks! – aexl Oct 7 at 8:10
19

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

  • 2
    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
  • It is possible set the log level in the directive error_log to debug so you can see the value of the variables and that block that are execute. Example error_log file.log debug – Victor Aguilar Jan 10 '17 at 0:55
  • 1
    notice thar empty variables are shown as - in the log, but are really empty in the nginx code, you should not check for - at any time. This sometimes confuse users. – higuita May 5 '17 at 12:56
6

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"
  • 2
    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
  • There is an echo_log directive in development. – Gajus Jul 14 '17 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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