我试图记录POST正文,并在http
子句中将$request_body
添加到log_format
中,但在我使用发送POST请求后,access_log
命令仅打印"-"作为正文
curl -d name=xxxx myip/my_location
我的日志格式(在http
子句中):
log_format client '$remote_addr - $remote_user $request_time $upstream_response_time '
'[$time_local] "$request" $status $body_bytes_sent $request_body "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
我的位置定义(在服务器条款中):
location = /c.gif {
empty_gif;
access_log logs/uaa_access.log client;
}
如何从curl打印实际POST数据?
Nginx不会解析客户端请求体,除非它真的需要,所以它通常不会填充$request_body
变量。
例外情况是:
- 它将请求发送到代理
- 或fastcgi服务器
因此,您确实需要将proxy_pass
或fastcgi_pass
指令添加到您的块中。
最简单的方法是将其作为代理服务器发送到Nginx本身,例如使用以下配置:
location = /c.gif {
access_log logs/uaa_access.log client;
# add the proper port or IP address if Nginx is not on 127.0.0.1:80
proxy_pass http://127.0.0.1/post_gif;
}
location = /post_gif {
# turn off logging here to avoid double logging
access_log off;
empty_gif;
}
如果您只希望接收一些密钥对值,那么限制请求正文大小可能是个好主意:
client_max_body_size 1k;
client_body_buffer_size 1k;
client_body_in_single_buffer on;
在使用empty_gif;
和curl进行测试时,我也收到了"405不允许"的错误(从浏览器上看是可以的),我将其切换到return 200;
以正确测试curl。
对于仍面临此问题的用户,请检查您的client_body_buffer_size
值。
如果req主体大小大于client_body_buffer_size
,那么nginx将用-
替换它。
哇,我做这件事很开心——我只是想找到一种方法来捕捉和记录特殊测试情况下的SSL帖子。不管怎样,我就是这么做的。
当然,日志格式需要$request_body。
在配置的http(非SSL)部分,我向自己转发-以强制进行日志记录。使用任何对你有利的路径。
listen 8081;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8081/kluge;
}
location /kluge {
root html;
index index.html index.htm;
access_log off;
}
在SSL部分(这是我主要进行测试的地方),也是一样的,但转发到http服务器:
location / {
root html;
index index.html index.htm;
# following required to get things to log...
proxy_pass http://127.0.0.1:8081/kluge;
}
工作良好,对配置的影响最小。当然,我可以通过不使用"/"路径,而是使用某些特定于我的测试要求的路径来降低它的侵入性。
感谢之前的帖子!通过这个我学到了很多关于nginx的知识。