使用nginx真正记录POST请求正文(而不是“ - ”) [英] Really logging the POST request body (instead of "-") with nginx

查看:1381
本文介绍了使用nginx真正记录POST请求正文(而不是“ - ”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试记录POST正文,并在<$ c $中将 $ request_body 添加到 log_format c> http 子句,但 access_log 命令只是在我使用以下命令发送POST请求后打印 - 作为正文:

I'm trying to log POST body, and add $request_body to the log_format in http clause, but the access_log command just prints "-" as the body after I send POST request using:

curl -d name=xxxx myip/my_location

我的log_format(在 http 子句中):

My log_format (in http clause):

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"';

我的位置定义(在服务器条款中):

My location definition(in server clause):

location = /c.gif {  
  empty_gif;  
  access_log logs/uaa_access.log client;  
}

如何从curl打印实际的POST数据?

How can I print the actual POST data from curl?

推荐答案

除非确实需要,否则Nginx不解析客户端请求体,因此它通常不会填充 $ request_body 变量。

Nginx doesn't parse the client request body unless it really needs to, so it usually does not fill the $request_body variable.

例外情况是:


  • 它将请求发送给代理,

  • 或fastcgi服务器。

所以你确实需要将 proxy_pass fastcgi_pass 指令添加到您的块中。

So you really need to either add the proxy_pass or fastcgi_pass directives to your block.

最简单的方法是将它作为代理服务器发送给Nginx本身,例如使用此配置:

The easiest way is to send it to Nginx itself as a proxied server, for example with this configuration:

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;  
}

如果您只希望收到一些密钥对值,则可能是好主意限制请求正文大小:

If you only expect to receive some key-pair values, it might be a good idea to limit the request body size:

client_max_body_size 1k;
client_body_buffer_size 1k;
client_body_in_single_buffer on;

我在使用 empty_gif; <进行测试时也收到405 Not Allowed错误/ code>和curl(浏览器没问题),我将其切换为返回200; 以正确测试卷曲。

I also received "405 Not Allowed" errors when testing using empty_gif; and curl (it was ok from the browser), I switched it to return 200; to properly test with curl.

这篇关于使用nginx真正记录POST请求正文(而不是“ - ”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆