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


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:


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.

| improve this answer | |
  • 32
    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
  • 38
    @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
  • 9
    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
  • omg.. so handy. thanks – Shaun Apr 8 at 3:23

You can return a simple string as HTTP response:

location /
    return 200 $document_root;

Note that you have to visit a page ending with .html if you do not want to change the page's headers.

| improve this answer | |
  • 2
    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. – CuriousGeorge May 9 '19 at 20:26
  • Using Postman, this works without any additional headers. Thanks! – aexl Oct 7 '19 at 8:10
  • I want to return multiple variables and solved it like this; return 200 "xforwardedfor:$proxy_add_x_forwarded_for--remote_addr:$remote_addr--scheme:$scheme--host:$host"; – kursat sonmez yesterday

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

| improve this answer | |
  • 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
  • 2
    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

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"
| improve this answer | |
  • 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
  • 1
    There is an echo_log directive in development. – Gajus Jul 14 '17 at 16:16
  • @Gajus Three years later now, and I googled for this, but can't find anything about echo_log. Do you know if it ever got out of development? – Randall Oct 30 at 18:14

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.