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 want to make a custom nginx access log that is not only JSON but also has JSON key:value pairs for every key=value in the query.

Making a JSON log for nginx is easy:

log_format main '{'
                 '"remote_addr": "$remote_addr",'
                 '"remote_user": "$remote_user",'
                 '"time_local":  "$time_local",'
                 '"request":     "$request",'
                 '"status":      "$status",'
                 '"body_bytes_sent": "$body_bytes_sent",'
                 '"http_referer": "$http_referer",' 
                 '"http_user_agent": "$http_user_agent"'
                 '}';

However lets say the request was GET /blah?foo=bar&hi=there%20mom

I'm looking for 2 additional things in the json

... 'http_user_agent':'chrome', 'foo':'bar', 'hi':'there mom' }

Can this be done? If so how can I make sure the resulting JSON is valid? (decodes URL escapes etc).

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Please have a look at the HttpSetMiscModule (GitHub Repo). You'll have to compile nginx yourself to include this module.

Afterwards you can do things like:

GET /blah?foo=bar&hi=there%20mom

set $foo $arg_foo;
set $hi $arg_hi;
set_quote_json_str $foo;
set_quote_json_str $hi;
log_format main '{'
    '"remote_addr": "$remote_addr",'
    '"remote_user": "$remote_user",'
    '"time_local":  "$time_local",'
    '"request":     "$request",'
    '"status":      "$status",'
    '"body_bytes_sent": "$body_bytes_sent",'
    '"http_referer": "$http_referer",' 
    '"http_user_agent": "$http_user_agent"'
    '"arg_foo": "$foo"'
    '"arg_hi": "$hi"'
'}';

Please note that I haven't tested this!

share|improve this answer
    
Interesting. However that requires you to know all the params ahead of time and/or have different configs for each web service (that has diff params) that you wish to log. I'm looking for a generic way to take every key/value and log it (access log) –  rynop Aug 17 '12 at 1:35
    
I understood that, but that's not possible as far as I'm aware. You could of course ask Igor itself but I doubt that there is a solution. You know what I'd do. Simply log $args and process it with the script that retrieves the JSON answer. It's very easy with JavaScript, jQuery, PHP, etc. to work with JSON. –  Fleshgrinder Aug 17 '12 at 21:58
    
Understand, thx. Gonna mark this as the answer as I think this is close as you can get with just nginx –  rynop Aug 19 '12 at 16:03

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.