由于我自己个人觉得nginx日志格式不是很友好,至少对我不是很友好,但nginx提供了日志格式编排的功能,所以可以结合自己的实际业务场景编排一下适合自己的格式输出,这样可以减少很多不必要的麻烦。
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include
mime.types;
default_type
application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 25M;
large_client_header_buffers 4 64k;
client_body_buffer_size 25M;
log_format myformat '
客户端地址: $remote_addr
客户端用户名称: $remote_user
访问时间和时区: $time_local
请求的URI和HTTP协议: $request
请求地址: $http_host
HTTP请求状态: $status
upstream状态: $upstream_status
发送给客户端文件内容大小: $body_bytes_sent
url跳转来源: $http_referer
用户终端浏览器等信息: $http_user_agent
SSL协议版本: $ssl_protocol
交换数据中的算法: $ssl_cipher
后台upstream的地址,即真正提供服务的主机地址: $upstream_addr
整个请求的总时间: $request_time
请求过程中,upstream响应时间: $upstream_response_time
';
upstream webgate{
server 172.172.0.14:8080;
}
server {
listen 80;
add_header 'Access-Control-Allow-Origin'$http_origin;
add_header 'Access-Control-Allow-Credentials''true';
add_header 'Access-Control-Allow-Methods''GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers''DNT,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
}
location /webgate {
proxy_pass http://webgate;
access_log /var/log/nginx/webgate.access.log myformat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root /usr/share/nginx/html;
index index.html;
add_header Cache-Control "no-cache";}
}
}
这里是我的nginx.conf配置,咱们主要看log_format myformat,log_format是声明日志格式,myformat是这个格式化的名称,也许自己表达能力有限,可以理解为java中的String abc,String是字符串类型,abc是这个字符串变量的名称。
还要注意下这里,access_log /var/log/nginx/webgate.access.log myformat;
access_log也是声明,声明访问成功的日志
/var/log/nginx/webgate.access.log 这个是访问成功的日志存放的路径
myformat 这个就是要使用那种日志格式,这里我就指定了自己的日志格式
尽全力解释一下这个意思:各位,我要开始记录日志了,我要记录的是访问成功的日志,我将日志存放在了/var/log/nginx/webgate.access.log,用的是myformat的这个格式,关于都记录什么内容及怎么显示,myformat中定义好了。
![](https://img-blog.csdnimg.cn/img_convert/a9c523537f71416e9a7e2c2c79e31d1b.png)
客户端地址: 192.168.1.50
客户端用户名称: -
访问时间和时区: 30/Jan/2023:02:04:02 +0000
请求的URI和HTTP协议: GET /webgate/userapi/public/authentication/jcaptcha.jpg HTTP/1.1
请求地址: 192.168.1.220
HTTP请求状态: 200
upstream状态: 200
发送给客户端文件内容大小: 1938
url跳转来源: http://192.168.1.220/
用户终端浏览器等信息: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36
SSL协议版本: -
交换数据中的算法: -
后台upstream的地址,即真正提供服务的主机地址: 172.172.0.14:8080
整个请求的总时间: 0.031
请求过程中,upstream响应时间: 0.032
这个表是整理出来日志中对应的变量都有哪些,大家可以参考自己实际场景使用进行取舍
参数 | 说明 | 示例 |
$remote_addr | 客户端地址 | 192.168.1.50 |
$remote_user | 客户端用户名称 | - |
$time_local | 访问时间和时区 | 30/Jan/2023:02:04:02 +0000 |
$request | 请求的URI和HTTP协议 | GET /webgate/userapi/public/authentication/jcaptcha.jpg HTTP/1.1 |
$http_host | 请求地址,即浏览器中你输入的地址(IP或域名) | 192.168.1.220/http://www.baidu.com |
$status | HTTP请求状态 | 200 |
$upstream_status | upstream状态 | 200 |
$body_bytes_sent | 发送给客户端文件内容大小 | 1938 |
$http_referer | url跳转来源 | |
$http_user_agent | 用户终端浏览器等信息 | Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36 |
$ssl_protocol | SSL协议版本 | TLSv1 |
$ssl_cipher | 交换数据中的算法 | RC4-SHA |
$upstream_addr | 后台upstream的地址,即真正提供服务的主机地址 | 172.172.0.14:8080 |
$request_time | 整个请求的总时间 | 0.031 |
$upstream_response_time | 请求过程中,upstream响应时间 | 0.032 |