NGINX-日志

作者dmxsp

7月 29, 2022

1.nginx日志详解

访问日志

/var/log/nginx/access.log

错误日志

/var/log/nginx/error.log

访问日志的格式

log_format 设置 nginx访问日志的格式

设置(开启)访问日志

access_log

2.log_format日志格式

配置文件

/etc/nginx/nginx.conf

log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

内容详情

$remote_addr:客户端IP地址
$remote_user:远程用户
$time_local:时间 11/May/2021:10:26:41 +0800
$request:用户请求报文的起始行 “GET/index.html HTTP/1.1”  (curl -v http://10.0.0.45:85/index.html)
$status:状态码
$body_bytes_sent:http响应报文的主体大小(文件大小) 字节 (服务器给你发送了1个多大的文件)(curl -v http://10.0.0.45:85/index.html  < Content-Length: 7)
$http_referer:从哪里跳转到你的网站 (从哪里跳转)分析用户的来源,精确投放广告(sem)
$http_user_agent:用户的代理(浏览器)
$http_x_forwarded_for:记录用户真实ip地址

access_log访问日志设置

Syntax access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];access_log off;
Default access_log logs/access.log combined
Context http, server, location, if in location, limit_except

path是路径
format 日志格式
gzip是否压缩 注压缩后日志最好命名为access.log.gz
fush 定时更新

应用案例

指定位置及格式

access_log /var/log/nginx/access.log main

压缩,日志先写入到缓存 每隔3s写入到磁盘

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.qiang.com.conf
server {
listen 80;
server_name dmxsp.qiang.com;
access_log /var/log/nginx/dmxsp.qiang.log.gz main gzip buffer=100 flush=3;

location / {
root /code/dmxsp;
index index.html index.php;
}
}

检查语法

[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

[root@web01 ~]$ systemctl reload nginx.service

测试

[root@web01 ~]$ ll /var/log/nginx/
-rw-r–r– 1 root root 122 Aug 1 13:36 dmxsp.qiang.log.gz

[root@web01 ~]$ curl 10.0.0.45

如何查看压缩包日志

[root@web01 ~]$ zcat /var/log/nginx/dmxsp.qiang.log.gz
10.0.0.45 – – [01/Aug/2022:13:36:33 +0800] “GET / HTTP/1.1” 200 6 “-” “curl/7.29.0” “-“

如何实时查看压缩包日志

[root@web01 ~]$ watch zcat /var/log/nginx/dmxsp.qiang.log.gz

让日志文件不显示404

查看日志

[root@web01 ~]$ tail -f /var/log/nginx/dmxsp.qiang.log

10.0.0.1 – – [01/Aug/2022:14:56:59 +0800] “GET /favicon.ico HTTP/1.1” 404 555 “-” “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3880.400 QQBrowser/10.8.4554.400” “-“

配置文件

[root@web01 ~]$ cat /etc/nginx/conf.d/dmxsp.qiang.com.conf
server {
listen 80;
server_name dmxsp.qiang.com;
access_log /var/log/nginx/dmxsp.qiang.log main;

location / {
root /code/dmxsp;
index index.html index.php;
}
location /favicon.ico {
access_log off;
}
}

检查语法

[root@web01 ~]$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

[root@web01 ~]$ systemctl reload nginx.service

测试

10.0.0.1 – – [01/Aug/2022:15:01:30 +0800] “GET / HTTP/1.1” 200 6 “-” “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3880.400 QQBrowser/10.8.4554.400” “-“

错误日志格式

Syntax error_log file [level]
Default error_log logs/error.log error
Context main , http , mail , stream , server ,location

level #日志格式
debug #最详细
notice #
error #等等

日志切割

定期把切割成一个新的文件(加上日期). 避免日志过大

如何使用

logrotate 命令 + /etc/logroate.d/配置文件

logrotate -f /etc/logrotate.d/nginx

配置文件

[root@web01 ~]$ cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {                         ##指定切割的文件
daily                                                    ##每天
missingok                                           ##如果对应日志不存在,跳过,不显示错误信息
rotate 52                                             ##最多保留多少个切割后的日志
compress                                            ##日志是否压缩 gzip                       
delaycompress                                   ##延迟一个周期,然后在进行压缩
notifempty                                         ##not if empty 如果日志是空的跳过
create 640 nginx adm                        ##日志权限,所有者
sharedscripts                                      ##
postrotate                                          ##在日志轮询(切割)之后,执行里面的命令
if [ -f /var/run/nginx.pid ]; then          ##
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}

nginx 定期切割原理

[root@web01 ~]$ ll /etc/cron.daily/
total 8
-rwx——. 1 root root 219 Apr 1 2020 logrotate

作者 dmxsp

发表回复

您的电子邮箱地址不会被公开。