在nginx日志的access log中记录post请求的参数值
背景:有时程序偶出现参数少了或没有提交到下一个链接Url里后出现问题,如何查呢,最好的办法是在nginx上的加post参数,以定位到问题才有可能对某个UIR的代码出现的问题进行排查。
og_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for';
access_log logs/test.access.log access;
注意放的位置在http里:nginx: [warn] the "log_format" directive may be used only on "http" level in /usr/local/nginx/conf/vhost/xxx.conf:59
nginx.conf
http里定义:wwwlog
log_format wwwlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for "$request_time"';
在include每个域名里后面加上wwwlog:
access_log /data/logs/access_mytv.log wwwlog;
于是在同样的access后这样写:
nginx.conf log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for'; 包含的justwinit.conf: access_log /data/logs/access_mytv.log access
日志如下:
202.108.16.77 - - [14/Jan/2015:10:45:45 +0800] "POST /partin/releaseinfo HTTP/1.1" 302 5 http://justwinit.cn/partin/showupload/activityid/47" "Mozilla/5.0 (Windows NT 6.1; rv:34.0) Gecko/20100101 Firefox/34.0" -
POST URI及参数:POST /partin/releaseinfo HTTP/1.1" 302 5 .....
来自的refer Uri:http://justwinit.cn/partin/showupload/activityid/47
这个access_log的log_format应该可以定义多个不同的名供不同日志的需求。
再就是,
get请求的参数就是存放在http header中的,所以修改header的大小限制 当然可以解决请求串过长的问题啦。
移动互联网行业开发过程中,服务端经常会需要检查是否收到请求,收到什么样的请求,最简单的办法就是看nginx的access log,常见的nginx配置中access log一般都只有GET请求的参数,而POST请求的参数却不行。
http://wiki.nginx.org/NginxHttpCoreModule#.24request_body
$request_body
This variable(0.7.58+) contains the body of the request. The significance of this variable appears in locations with directives proxy_pass or fastcgi_pass.
正如上文件所示,只需要使用$request_body即可打出post的数据,在现存的server段加上下面的设置即可:
log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" "$http_user_agent" $http_x_forwarded_for';
access_log logs/test.access.log access;