nginx日志 打印 post body

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/glw0223/article/details/88354474

背景

当客户端要上传的日志内容比较大时,在url里加参数就会显得比较冗余,这时就需要在body里,通过post的方式上传,如果post的内容是json的话,更容易扩展。

修改配置文件 nginx.conf

主要修改的内容如下,具体位置参见后面的示例
1、

   log_format post_log   '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" '
                         '"$http_x_forwarded_for" "$request_body"';

2、

access_log /yourpath/client_report.log post_log;
记录一个线上使用的nginx.conf

user  root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

   log_format post_log   '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" '
                         '"$http_x_forwarded_for" "$request_body"';
    
    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            dav_methods PUT;
            client_max_body_size 1024m;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ /v1/log/sdk/* {
            lua_need_request_body on;
            content_by_lua 'local s = ngx.var.request_body';
            client_body_buffer_size 2m;
            set $svr     "on"; #开启或关闭copy功能
            client_body_in_single_buffer on;
            client_max_body_size 2m;
            access_log /yourpath/client_report.log post_log;
        }
    }
}

其他

现在一般可以使用openresty,它包装了nginx很多东西,使用方便,而且更新也比较快。

猜你喜欢

转载自blog.csdn.net/glw0223/article/details/88354474