29 Nginx的http块自定义服务日志
29 Nginx的http块自定义服务日志
29.1 自定义服务日志
Nginx 中日志的类型分access.log、error.log
access.log:记录用户所有的访问请求
error.log:记录nginx本身运行时的错误信息,不会记录用户的访问请求
Nginx服务器支持对服务日志的格式、大小、输出等进行设置,需要使用两个指令,分别是 access_log 和 log_format 指令
监控日志,浏览器访问:http://10.0.0.100/ ,后台日志会刷新
[root@nginx-100 ~]# tail -f /usr/local/nginx/logs/access.log 10.0.0.1 - - [22/Apr/2026:18:26:19 +0800] "GET /favicon.ico HTTP/1.1" 404 532 "http://10.0.0.100/" \ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"
29.2 access_log
access_log:设置用户访问日志的相关属性,path:access_log 存放的路径、format:日志格式、buffer=size:日志文件的大小
| 语法 | access_log path[format[buffer=size]] |
| 默认值 | access_log logs/access.log combined; |
| 位置 | http、server、location |
[root@nginx-100 ~]# cd /usr/local/nginx/conf/ [root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf .......... http { include mime.types; default_type application/octet-stream; #access_log logs/access.log main; access_log logs/my.log; sendfile on; keepalive_timeout 65; .......... [root@nginx-100 /usr/local/nginx/conf]# ../sbin/nginx -t && ../sbin/nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/ ,access.log 不再刷新日志,而是增加了 my.log 并添加了对应的日志
[root@nginx-100 ~]# cd /usr/local/nginx/logs/ [root@nginx-100 /usr/local/nginx/logs]# ls -ltr total 32 -rw-r--r-- 1 nobody root 9225 Apr 21 18:38 access1.log -rw-r--r-- 1 root root 5 Apr 22 18:24 nginx.pid -rw-r--r-- 1 root root 213 Apr 22 18:26 access.log -rw-r--r-- 1 nobody root 6950 Apr 22 18:36 error.log -rw-r--r-- 1 root root 183 Apr 22 18:37 my.log [root@nginx-100 /usr/local/nginx/logs]# tail -f my.log 10.0.0.1 - - [22/Apr/2026:18:37:39 +0800] "GET / HTTP/1.1" 304 0 "-" \ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"
29.3 log_format
log_format:设置日志的输出格式,name 与上面的 format 名称一致,日志中格式就可以被引用、escape:输出内容格式
| 语法 | log_format name [escape=default|json|none] string...; |
| 默认值 | log_format combined "..."; |
| 位置 | http |
# 修改配置文件 [root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf include /usr/local/nginx/conf/main.conf; .......... http { include mime.types; default_type application/octet-stream; log_format myformat '=======> This is My format'; access_log logs/my.log myformat; sendfile on; keepalive_timeout 65; .......... # 重新加载配置文件 [root@nginx-100 /usr/local/nginx/conf]# ../sbin/nginx -t && ../sbin/nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/
[root@nginx-100 /usr/local/nginx/conf]# tail -f ../logs/my.log 10.0.0.1 - - [22/Apr/2026:18:37:39 +0800] "GET / HTTP/1.1" 304 0 "-" \
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36" =======> This is My format
日志内容记录的是浏览器上Request Headers中的 User-Agent 的内容

29.4 http_user_agent配置
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf .......... http { include mime.types; default_type application/octet-stream; log_format myformat '=======> This is My format: $http_user_agent'; access_log logs/my.log myformat; sendfile on; keepalive_timeout 65; .......... [root@nginx-100 /usr/local/nginx/conf]# ../sbin/nginx -t && ../sbin/nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/
[root@nginx-100 /usr/local/nginx/logs]# tail -f my.log 10.0.0.1 - - [22/Apr/2026:18:37:39 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 \
(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36" =======> This is My format =======> This is My format =======> This is My format: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) \ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36
———————————————————————————————————————————————————————————————————————————
无敌小马爱学习
浙公网安备 33010602011771号