Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

3 Answers 3

up vote 2 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:

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";
    ...
}

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

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.

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 at 14:43

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

Your Answer

 
discard

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.