30

我正在尝试记录 POST 正文,并添加$request_bodylog_formatinhttp子句,但access_log在我使用以下命令发送 POST 请求后,该命令仅将“-”打印为正文:

curl -d name=xxxx myip/my_location

我的 log_format(在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 数据?

4

1 回答 1

63

Nginx 不会解析客户端请求体,除非它真的需要,所以它通常不会填充$request_body变量。

例外情况是:

  • 它将请求发送到代理,
  • 或 fastcgi 服务器。

因此,您确实需要将proxy_passorfastcgi_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 Not Allowed”错误(从浏览器中可以),我将其切换为return 200;使用 curl 进行正确测试。

于 2013-07-20T18:15:43.203 回答