log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status",'
'"request_uri":"$request_uri",'
'"uri":"$uri",'
'"document_uri":"$document_uri"}';
- 自定义log,其中可以设置很多的参数,以便出现问题时的排查
- 今天我们来说说uri、request_uri这两个
- 首先在nginx.conf中加入log_format,日志格式化
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6c334996e9df64e6789588dac2de38d3.png)
- 然后修改https.www.flighting.top.conf,为了测试方便,我们先去掉防盗链的设置
server {
listen 443 ssl;
server_name www.flighting.top;
ssl_certificate /etc/nginx/crt/test.crt;
ssl_certificate_key /etc/nginx/crt/test.key;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
location / {
proxy_pass http://tomcat;
access_log /var/log/nginx/access.log json;
}
}
access_log /var/log/nginx/access.log json;
- 记录日志,指定文件夹及格式
- 为测试方便,我们直接访问图片https://47.101.201.179/tomcat.png?test=1,后面的参数是随便加上的
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6c3ba6babf3daadba6260d88dd9b6088.png)
- 我们来看下log
cat /var/log/nginx/access.log
{
"@timestamp":"2019-10-28T16:00:55+08:00",
"host":"172.19.133.248",
"clientip":"58.247.101.102",
"size":5103,
"responsetime":0.001,
"upstreamtime":"0.000, 0.001",
"upstreamhost":"127.0.0.1:8086, 127.0.0.1:8085",
"http_host":"47.101.201.179",
"url":"/tomcat.png",
"xff":"-",
"referer":"-",
"agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
"status":"200",
"request_uri":"/tomcat.png?test=1",
"uri":"/tomcat.png",
"document_uri":"/tomcat.png"
}
- $request_uri:/tomcat.png?test=1
- $uri :/tomcat.png
- 大家可以看到区别了,很清晰
- 关注公众号"双城人",搬砖过程遇到的问题,大家一起探讨,资源共享
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f35463b1dc70c0348bacb140479cce63.png)