nginx打印响应内容

oprenresty打印proxy后的响应内容,在默认的log_format并没有提供,只能借助lua来做,定义一个变量,然后再日志里面打印即可。

http {
    log_format accessupdate '$remote_addr [$time_local] $request $status $body_bytes_sent $request_body $resp_body';

    server {
        listen 80;
        location / {
            access_log logs/access.log accessupdate;
            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
            ';
        }
    }
}

网上有更简单的方案,打印到错误日志里面,但是这种会失去其它log_format的指导作用,这个方式我没有尝试过。

error_log /tmp/nginx.resp.info.log info;

location / {
    proxy_pass http://vpsea.flvcd.com/;
    body_filter_by_lua 'ngx.log(ngx.INFO, ngx.arg[1])';
}

参考链接 :

nginx打印响应内容(reponse body) :http://ciika.com/category/webserver/

发布了364 篇原创文章 · 获赞 66 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/105544340