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

I am trying to print the response data for development/debugging purposes. I couldn't find a similar question or any info on this. If you do, please add a comment.

share|improve this question
    
Do you mean something like this? stackoverflow.com/questions/4939382/… –  Taylor Jasko Feb 19 '12 at 4:39
1  
No. That is the request body but I want to spit out the response body. Thanks. –  vamsu Feb 19 '12 at 7:51
add comment

3 Answers

up vote 1 down vote accepted

Use body_filter_by_lua to assign request body to a nginx variable, here is an example:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent" $request_time req_body:"$request_body" resp_body:"$resp_body"';

    server {
        listen 8082;
        access_log logs/access.log log_req_resp;

        lua_need_request_body on;

        set $resp_body "";
        body_filter_by_lua '
            local resp_body = string.sub(ngx.arg[1], 1, 1000)
            ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
            if ngx.arg[2] then
                ngx.var.resp_body = ngx.ctx.buffered
            end
        ';

        location / {
            echo "Hello World!";
        }
    }
}
share|improve this answer
add comment

use ngx_lua module

like this

body_filter_by_lua 'ngx.log(ngx.CRIT,ngx.arg[1])';

in the right location

share|improve this answer
    
I think I'd prefer to use ngx.DEBUG. CRIT might get printed to the console, emailed to the whole sysadmin team, etc... –  Michael Hampton Feb 15 '13 at 8:08
add comment

I've found this one for example, and I think the echo module can also do it.

share|improve this answer
1  
The echo module and the example provided talk about logging the request headers/body but I want to log the response body. Thanks petermolanar. –  vamsu Feb 21 '12 at 19:55
add comment

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.