首先看了一些文章,request_body为空是因为nginx 尚未读取请求体的时候,或者请求体有一部分或者全部缓冲到临时文件的时候,$request_body 和 $echo_request_body 都将是空值。所以需要增加配置
处理办法在nginx.conf配置文件中添加了两个配置项:
fastcgi_buffers 32 8k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答
client_body_buffer_size 1024k; #缓冲区代理缓冲用户端请求的最大字节数
之后发现有return命令时lua代码不会执行,可以实现return 返回,但是请求体request_body还是为空。
所以只能用lua代码来实现记录请求体和返回状态码到客户端
log_format post_data escape=json
'{'
'"time_local":"$time_local",'
'"remote_addr":"$remote_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"args": "$args",'
'"data":"$request_body",'
'"http_user_agent":"$http_user_agent"'
'}';
sendfile on;
keepalive_timeout 65;
init_by_lua 'cjson = require "cjson"';
server {
listen 80;
server_name localhost;
fastcgi_buffers 32 8k;
client_body_buffer_size 5m;
client_max_body_size 10m;
client_body_in_single_buffer on;
lua_need_request_body on;
location /ad/ad_click
{
if ($request_method ~* POST)
{
content_by_lua_block
{
local s = ngx.var.request_body ;
ngx.say(cjson.encode({ret = 0}));
}
access_log /data2/data/ad_log/ad.log post_data ;
}
}
使用postman发出请求,可以看到有返回值
同时日志里面记录请求体