日志文件
错误日志
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
格式:error_log 文件路径 日志级别;
源码安装默认文件路径:
/usr/local/nginx/logs/error.log;yum安装默认路径:/var/log/nginx/error.log;日志级别不指定默认为:error;
错误日志的日志级别格式是固定的,不能自定义;
可以配置在main、http、server、location段里;
日志级别:
debug:最详细的日志级别,用于调试目的。记录有关每个请求的所有细节,包括请求头、响应头和其他调试信息。在生产环境中通常不使用这个级别,因为会产生大量日志数据。debug模式需要在源码编译nginx时开启
--with-debug参数后才能调用。
info:记录关键事件和状态变化,通常用于生产环境中。这个级别下的日志包含 HTTP 请求处理的概要信息,例如请求的处理时间、状态码等。notice:用于记录一般性的重要信息。通常记录系统运行状态、配置错误、非致命错误等信息。warn:记录警告信息,表示可能会发生问题的事件。例如,磁盘空间不足、某些请求被拒绝等情况。error:记录错误事件,在这个级别下的日志会包含出现错误的请求、客户端断开连接、服务器错误等重要错误信息。crit:记录紧急情况,例如服务不可用、内部错误等。这些日志通常是需要立即关注和处理的。alert:记录需要立即处理的严重错误,通常需要立即通知系统管理员进行处理。emerg:最高级别的日志,用于紧急情况,表示系统已经不可用或正在发生严重错误,需要立即采取行动。
访问日志
访问日志格式
可将下列内容配置在main、http、server、location段里:
# 默认格式
access_log logs/access.log main;
格式:access_log 文件路径 格式名称;
访问日志可以自定义格式,调用时在路径后面加上格式名称即可;
若不配置log_format或者不在access_log配置中指定log_format;
日志格式自定义:
$ vim /usr/local/nginx/conf/nginx.conf
# 默认格式
'$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent"';
# combined_realip
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
# main
log_format main '$remote_addr [$time_local]'
'$host "$request_uri" $status "$request"'
'"$http_referer" "$http_user_agent" "$request_time"';
默认格式:适合用于记录通用的访问日志,可以满足大多数场景的需求。
combined_realip:类似于第一个格式,但在记录客户端 IP 地址时考虑了代理服务器的影响。客户端 IP 和代理 IP 都被记录,适合用于跟踪来自代理服务器的请求。这种格式适合用于记录通过代理服务器的请求,以及需要记录代理 IP 地址的情况。
main:这个格式与前两种格式相比,增加了$request_time,记录了请求处理的时间。这种格式适合用于需要记录请求处理时间的场景,可以帮助分析请求的处理效率和性能。
常见变量:
$time_local:通用日志格式下的本地时间;(服务器时间)$remote_addr:客户端(用户)IP地址$remote_user:客户端用户名称,认证通过为“*”,认证失败为“-”$status:请求状态码,如200,404,301,302等$body_bytes_sent:服务器发送给客户端的字节数,不包括响应头的大小,只包括响应体的大小$bytes_sent:服务器发送给客户端的总字节数,包括响应头和响应体的大小$request_length:请求的长度,包括请求行、请求头和请求体的大小$request_time:请求处理的时间,单位为秒,精度可以到毫秒$upstream_addr:集群轮询地址$upstream_response_time:指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间$request:请求方式(GET或者POST等)+URL(包含$request_method,$host,$request_uri)$http_user_agent:用户浏览器标识$http_host:请求的url地址(目标url地址)的host$host:等同于$http_host$http_referer:来源页面,即从哪个页面转到本页,如果直接在浏览器输入网址来访问,则referer为空$uri:请求中的当前URI(不带请求参数,参数位于args),不同于浏览器传递的request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。$document_uri:等同于$uri$request_uri:比uri多了参数,即uri+$args$http_x_forwarded_for:如果使用了代理,这个参数会记录代理服务器的ip和客户端的ip
配置
server
{
listen 80;
server_name www.lwz.com;
root /data/wwwroot/www.lwz.com;
index index.html index.php;
access_log /data/logs/www.lwz.com_access.log main;
}
#说明:若不指定log_format,则按照默认的格式写日志。
访问日志过滤
一个网站,会包含很多元素,尤其是有大量的图片、js、css等静态元素。
这样的请求其实可以不用记录日志。
# 通过下面配置,将静态文件请求的日志过滤掉
location ~* ^.+\.(gif|jpg|png|css|js)$
{
access_log off;
}
#或
location ~* ^.+\.(gif|jpg|png|css|js)$
{
access_log /dev/null;
}
示例
[root@rocky vhosts]# cat 1.com.conf
server
{
listen 80;
server_name www.1.com;
root /data/wwwroot/1.com;
index index.html;
access_log logs/www.1.com.log main;
error_log /tmp/1.com.error.log debug;
location /auth/
{
auth_basic "user:passwd";
auth_basic_user_file /usr/local/nginx/conf/passwd;
}
location ~* '(gif|jpg|css)'
{
access_log logs/1.com.log test;
}
}
[root@rocky vhosts]# curl -x127.0.0.1:80 www.1.com/1.jpg
[root@rocky vhosts]# tail ../../logs/www.1.com.log
[root@rocky vhosts]# tail ../../logs/1.com.log
29/Aug/2022:13:40:15 +0800 127.0.0.1 404 www.1.com "/1.jpg""-" "curl/7.61.1"limit_conn_zone\x7F\x00\x00\x01zone=test:10m
# 修改日志为off
[root@rocky vhosts]# cat 1.com.conf
server
{
listen 80;
server_name www.1.com;
root /data/wwwroot/1.com;
index index.html;
access_log logs/www.1.com.log main;
error_log /tmp/1.com.error.log debug;
location /auth/
{
auth_basic "user:passwd";
auth_basic_user_file /usr/local/nginx/conf/passwd;
}
location ~* '(gif|jpg|css)'
{
access_log off;
}
}
[root@rocky vhosts]# !curl
curl -x127.0.0.1:80 www.1.com/1.jpg
[root@rocky vhosts]# tail ../../logs/www.1
www.123.com.log www.1.com.log
[root@rocky vhosts]# tail ../../logs/www.1.com.log
# 修改为/dev/null
[root@rocky vhosts]# !cat
cat 1.com.conf
server
{
listen 80;
server_name www.1.com;
root /data/wwwroot/1.com;
index index.html;
access_log logs/www.1.com.log main;
error_log /tmp/1.com.error.log debug;
location /auth/
{
auth_basic "user:passwd";
auth_basic_user_file /usr/local/nginx/conf/passwd;
}
location ~* '(gif|jpg|css)'
{
access_log /dev/null;
}
}
[root@rocky vhosts]# !curl
[root@rocky vhosts]# !tail
tail ../../logs/www.1.com.log
[root@rocky vhosts]# tail ../../logs/1.com.log
日志切割
shell脚本切割
#!/bin/bash
logdir=/var/log/nginx # 定义日志路径
prefix=`date -d "-1 day" +%y%m%d` # 定义切割后的日志前缀
cd $logdir
for f in `ls *.log`
do
mv $f $f-$prefix # 把日志改名
done
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null # 生成新的日志
bzip2 *$prefix # 压缩日志
find . -type f -mtime +180 |xargs /bin/rm -f # 删除超过180天的老日志
logrotate切割
使用logrotate日志分割工具。
创建文件
$ vim /etc/logrotate.d/nginx /usr/local/nginx/logs/*.log { daily rotate 30 missingok notifempty compress sharedscripts postrotate /bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null endscript }-daily 每天分割
-rotate 30 保留30个
-missingok 丢失文件不报错
-notifempty 不为空才分割
-compress 压缩
-sharedscripts 共享脚本
-postrotate 表示当切割之后要执行的命令
-endscript 结束脚本
执行命令分割
logrotate -f /etc/logrotate.d/nginx
效果
[root@lwz vhosts]# ll /usr/local/nginx/logs/ 总用量 12 -rw-r--r-- 1 nobody root 428 1月 12 17:51 access.log -rw-r--r-- 1 nobody root 428 1月 12 17:51 access.log.1.gz -rw-r--r-- 1 nobody root 0 1月 13 15:39 error.log -rw-r--r-- 1 root root 282 1月 12 17:51 error.log.1.gz