nginx 获取 post body值

1.现象

   在nginx中想利用$request_body命令获取post请求的body参数,并落日志,但是发现该变量值为空,查看官网中对$request_body的描述如下:

$request_body
    request body

    The variable’s value is made available in locations processed by the proxy_pass, fastcgi_pass, uwsgi_pass, and scgi_pass directives when the request body was read to a memory buffer.

意思是只有location中用到proxy_pass,fastcgi_pass,scgi_pass命令时,该变量才有值。


2.使用proxy_pass,fastcgi_pass, scgi_pass等命令获取$request_body值

试了下用proxy_pass,的确可以。配置如下:

worker_processes  1;        #nginx worker 数量
error_log logs/error.log;   #指定错误日志文件路径
events {
    worker_connections 1024;
}

http {
    log_format  dm  ' "$request_body" ';

    upstream bk_servers_2 {
        server 127.0.0.1:6699;
    }

    server {
        listen 6699;
        location /post/ {
            proxy_pass http://bk_servers_2/api/log/letv/env;
            access_log /home/shuhao/openresty-test/logs/post.log dm;
        }
        location /api/log/letv/env {
            return 202;
        }
    }
}
使用curl命令模拟post请求

curl -i  -d "arg1=1&arg2=2" "http://127.0.0.1:6699/post/"

日志用打印出结果:

 "arg1=1&arg2=2" 

3.使用lua获取$request_body值

条件:使用openresty或者nginx编译了lua模块。

方法:

    server中使用lua_need_request_body on; 或者在location lua代码块中使用 ngx.req.read_body()

注意:

    1)lua代码块中必须有执行语句,否则lua不执行,无法获取request_body;

    2)不要使用return 200;等命令,有return命令,lua代码不执行。

worker_processes  1;        #nginx worker 数量
error_log ~/openresty-test/logs/error.log debug;   #指定错误日志文件路径
events {
    worker_connections 1024;
}

http {
    log_format  dm  '"$request_body"';
    lua_need_request_body on;
    server {
        listen 6699;
        location /post/ {
            content_by_lua '
                ngx.say("-------")
                ngx.req.read_body()
            ';
            access_log ~/openresty-test/logs/post.log dm;
            #return 200;
        }
    }
}

4. 自定义变量存放request body

方法:

    1)在server 块中使用set $resp_body ""; 声明变量;

    2)在location使用  ngx.var.resp_body = ngx.req.get_body_data() or "-"    为变量赋值

worker_processes  1;        #nginx worker 数量
error_log /home/shuhao/openresty-test/logs/error.log debug;   #指定错误日志文件路径
events {
    worker_connections 1024;
}

http {
    log_format  dm  ' "$request_body"  --  "$resp_body"';
    lua_need_request_body on;
    server {
        listen 6699;
        set $resp_body "";
        location /post/ {
            lua_need_request_body on;
            content_by_lua '
                local resp_body = ngx.req.get_body_data() or "-"                                                                                                    
                ngx.var.resp_body = resp_body
            ';
            access_log /home/shuhao/openresty-test/logs/post.log dm;
            #return 200;
        }
    }
}










  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cenos 6.7 x86_64 yum安装 yum localinstal nginx-1.6.3-1.x86_64.rpm 定制包安装过程 1: FPM 打包工具安装 修改yum源: 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 安装阿里云yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 安装依赖包 yum -y install ruby rubygems ruby-devel 添加阿里云的Rubygems 仓库 gem sources -a http://mirrors.aliyun.com/rubygems/ #移除原生Ruby仓库 gem sources --remove http://rubygems.org/ 指定安装版本 gem install fpm -v 1.3.3 wget http://nginx.org/download/nginx-1.9.7.tar.gz wget http://nginx.org/download/nginx-1.6.3.tar.gz mkdir -p /application/tools cd /application/tools/ find /var/cache/yum/ -name "*rpm" yum install pcre-devel openssl-devel find /var/cache/ -type f -name '*rpm' find /var/cache/ -type f -name '*rpm'|xargs cp -t /tmp/ cd /tmp tar zcf nginx_yum.tar.gz *.rpm sz nginx_yum.tar.gz cd /application/tools/ useradd nginx -M -s /sbin/nologin tar zxf nginx-1.6.3.tar.gz ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module make make install ln -s /application/nginx-1.6.3/ /application/nginx /application/nginx/sbin/nginx ss -lntup|grep nginx ps -ef|grep nginx|grep -v grep netstat -lntup|grep nginx|grep -v grep curl 127.0.0.1 mkdir -p /server/scripts cd /server/scripts/ fpm -s dir -t rpm -n nginx -v 1.6.3 -d 'pcre-devel,openssl-devel' --post-install /server/scripts/nginx_rpm.sh -f /application/nginx-1.6.3/ 检查 rpm -qpl nginx-1.6.3-1.x86_64.rpm [root@nginx tools]# rpm -qpl nginx-1.6.3-1.x86_64.rpm /application/nginx-1.6.3/client_body_temp /application/nginx-1.6.3/conf/fastcgi.conf /application/nginx-1.6.3/conf/fastcgi.conf.default /application/nginx-1.6.3/conf/fastcgi_params /application/nginx-1.6.3/conf/fastcgi_params.default /application/nginx-1.6.3/conf/koi-utf /application/nginx-1.6.3/conf/koi-win /application/nginx-1.6.3/conf/mime.types /application/nginx-1.6.3/conf/mime.types.default /a

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值