首页 > 系统相关 >[转帖]nginx利用request_body记录POST body(location中用proxy_pass)

[转帖]nginx利用request_body记录POST body(location中用proxy_pass)

时间:2024-02-21 16:01:26浏览次数:31  
标签:body log request server 转帖 nginx pass post

https://www.cnblogs.com/freedom-try/p/14699538.html

  

1.完整过程

1.1 在nginx.confhttp里面添加配置如下:

http {
	...
	log_format postdata escape=json '$remote_addr - $remote_user [$time_local] "$request" 
		                        '$status $body_bytes_sent "$http_referer" '
		                        '"$http_user_agent" "$http_x_forwarded_for" "$request_body"';
	upstream bk_server {
		server 127.0.0.1:12345;
	}
	server {
		listen 12345;
		location / {
		        proxy_pass http://bk_server/test;
                        access_log /var/log/nginx/post.log postdata;
		}
		location /test {
		        return 202;
		}
	}
	...
}

检查nginx配置语法有无错误

nginx -t

若无语法错误,则reload nginx配置,使最新的nginx配置生效

nginx -s reload

1.2 使用curl命令模拟post请求

curl -i  -d "arg1=1&arg2=2&hi=你好" "http://127.0.0.1:12345"

查看日记:

tail -n 10 /var/log/nginx/post.log

得到结果:

...省略其他参数的值...  "arg1=1&arg2=2&hi=你好"

如果nginx装在公网服务器上,那么请将127.0.0.1换成公网ip

2.说明

2.1 log_format配置

log_format官方文档
log_format 语法:

log_format name [escape=default|json|none] string ...;
  • postdata: 名称
  • escape=json: 在配置日志格式时加上此参数可以不转义变量内容,这里为了显示POST body里面的中文。(escape参数,到版本1.11.8才有,escape参数的none值到1.13.10版本才有)
  • $request_body: 只有location中用到proxy_pass,fastcgi_pass,scgi_pass命令时,该变量才有值。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.

2.1.1

test

2.2 bk_server

bk_server是为了使用proxy_pass而设置的。

3.显示HTTPS里面的POST body(可选)

如网站已使用HTTPS,那么配置如下:

3.1 除了需要把access_log那一行注释掉之外,步骤1中的配置不变。

3.2 其他配置如下

server {
        # Redirect all http requests to https.
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}
server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name www.your_domain your_domain;
        ...
        # Record POST body
        location /post {
                proxy_pass http://127.0.0.1:12345;
                access_log /var/log/nginx/post.log postdata; #这里设置之后,需要把步骤1里面的access_log那一行注释掉
        }
        ...
}

检查nginx配置语法有无错误

nginx -t

若无语法错误,则reload nginx配置,使最新的nginx配置生效

nginx -s reload

使用curl命令模拟post请求

curl -i  -d "arg1=1&arg2=2&hi=你好" "https://your_domain/post"

查看日记:

tail -n 10 /var/log/nginx/post.log

得到结果:

...省略其他参数的值...  "arg1=1&arg2=2&hi=你好"

参考: nginx记录post body/payload数据

  分类: O&M 标签: post body , Nginx , proxy_pass , $request_body , POST data

标签:body,log,request,server,转帖,nginx,pass,post
From: https://www.cnblogs.com/jinanxiaolaohu/p/17982358

相关文章

  • npm 报错 npm ERR! request to https://registry.npm.taobao.org/three failed, reaso
    1.问题描述npm使用淘宝镜像安装报错npmERR!codeCERT_HAS_EXPIREDnpmERR!errnoCERT_HAS_EXPIREDnpmERR!requesttohttps://registry.npm.taobao.org/xxxfailed,reason:certificatehasexpired2.错误原因错误提示已经告诉原因是淘宝镜像过期了!其实,早在2021......
  • python实战:用requests+json抓取接口
    一,安装requests1,用pip安装(venv)liuhongdi@192news%pip3installrequests2,查看所安装库的版本:(venv)liuhongdi@192news%pip3showrequestsName:requestsVersion:2.31.0Summary:PythonHTTPforHumans.Home-page:https://requests.readthedocs.ioAu......
  • [转帖]一次搞定 Unicode、字节序、UTF-*
    茫茫人海中与你相遇相信未来的你不会很差作者:Tomson                                      来源:https://segmentfault.com/a/1190000038171151什么是字符集顾名思义,字符集就是字符的......
  • python实战:用requests+做爬虫
    一,安装requests1,用pip安装(venv)liuhongdi@192news%pip3installrequests2,查看所安装库的版本:(venv)liuhongdi@192news%pip3showrequestsName:requestsVersion:2.31.0Summary:PythonHTTPforHumans.Home-page:https://requests.readthedocs.ioAu......
  • 【转帖】阿里云ssh远程连接短时间就会断掉的解决方案
    https://zhuanlan.zhihu.com/p/423385471 本文已收录公众号《极客运维之家》,欢迎关注公众号一起交流学习文章目录问题重现问题分析问题解决打开sshd的配置文件修改如下参数重启服务:补充总结问题重现阿里云服务器,使用Finalshell远程连接,在操作中没有出现任务......
  • requests使用代理
    获取代理池ipimportrequestsres=requests.get('http://demo.spiderpy.cn/get/?type=https')print(res.json())print(res.json()['proxy'])#112.30.155.83:12792使用代理ip发送请求header={'User-Agent':'Mozilla/5.0(WindowsNT......
  • requests实现模拟登录
    发送登录请求importrequestsdata={'username':'用户名','password':'密码','captcha':'3333','remember':'1','ref':'http://www.aa7a.cn/'......
  • requests的基本使用
    首先需要导入模块importrequests携带get请求方式params={'xxx':'yyy',}res=requests.get('xxx',params=params)print(res.text)#打印响应体内容携带请求头headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;......
  • java 获取请求request,并返回请求的url
    StringwebStr=getRequest().getScheme()+"......
  • Failed to execute ‘requestFullscreen‘ on ‘Element‘
    来源:http://www.shanhubei.com/archives/13628.html浏览器无法自启动全屏模式报错信息:Failedtoexecute‘requestFullscreen’on‘Element’:APIcanonlybeinitiatedbyausergesture.翻译:无法在element上执行requestFullscreen方法,这个API只有用户主动行为才可以触发......