165

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

lulalala
  • 1,797

6 Answers6

244

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.

yuvilio
  • 2,572
76

You can return a simple string as HTTP response:

location / {
    add_header Content-Type text/plain;
    return 200 $document_root;
}

Or use interpolation for watching multiple variables at the same time:

location / {
    add_header Content-Type text/plain;
    return 200 "document_root: $document_root, request_uri: $request_uri";
}
Nick Roz
  • 103
  • 4
25

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

mgorven
  • 31,399
8

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"
7

none of these answer the question as asked (log)

However, @Victor Aguilar - has a comment on this answer, that should be an answer! It says how to log variables, and it works. Thanks!

https://serverfault.com/a/404628/400075

i.e. in /etc/nginx/nginx.conf

error_log /var/log/nginx/error.log debug;

results in the following type of logging:

2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "SCRIPT_FILENAME: /usr/lib/cgit/cgit.cgi/something.git/cgit.cgi"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "QUERY_STRING"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "QUERY_STRING: "
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "REQUEST_METHOD"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "GET"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "REQUEST_METHOD: GET"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "CONTENT_TYPE"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "CONTENT_TYPE: "
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "CONTENT_LENGTH"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "CONTENT_LENGTH: "
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "SCRIPT_NAME"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/cgit/cgit.cgi/something.git/cgit.cgi"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "SCRIPT_NAME: /cgit/cgit.cgi/something.git/cgit.
cgi"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "REQUEST_URI"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/cgit/cgit.cgi/something.git/"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "REQUEST_URI: /cgit/cgit.cgi/something.git/"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "DOCUMENT_URI"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/cgit/cgit.cgi/something.git/"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "DOCUMENT_URI: /cgit/cgit.cgi/something.git/"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "DOCUMENT_ROOT"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script var: "/usr/lib"
2021/03/06 15:14:48 [debug] 2550#2550: *1 fastcgi param: "DOCUMENT_ROOT: /usr/lib"
2021/03/06 15:14:48 [debug] 2550#2550: *1 http script copy: "SERVER_PROTOCOL"
2021/03/06 15:14:48 [debug] 2550#2550: *....

0

The 1st question: how to log a variable?

You can set error_log to debug, and then use if with a regexp that matches everything, eg ^. This method works like a built-in echo.

You can even "name" your log just like ^|some_name.

For example:

server {
    error_log   /dev/stderr debug;
    if ( $variable1 ~ "^|varlable1" ) { set $a ''; }
    location {
        if ( $variable2 ~ "^|var2" ) { set $a ''; }
        if ( $variable3 ~ "^|var3" ) { set $a ''; }
    }
}

Note: just like lots of other things in nginx, if can be used only in server {} or location {}

example output:

"^|var3" matches "https://example.com" while ...

The 2nd question: Which file it will go to?

error_log <the_file_you_want_it_to_go_to> debug;