Nginx的日志主要分为两类,用于记录不同维度的运行信息:
访问日志的默认格式为combined(组合格式),包含客户端请求的关键信息。通过log_format指令可自定义日志字段,满足个性化需求。
log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
对应日志样例:
192.168.1.100 - - [02/Oct/2025:10:00:00 +0800] "GET /index.html HTTP/1.1" 200 1024 "http://example.com/ref" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-"
| 字段变量 | 含义说明 |
|---|---|
$remote_addr |
客户端的IP地址(若经过代理,显示代理服务器IP) |
$remote_user |
客户端认证的用户名(未认证则显示-) |
$time_local |
本地时间(格式:DD/MMM/YYYY:HH:MM:SS +TZ,如02/Oct/2025:10:00:00 +0800) |
$request |
完整的请求行(包括方法、路径、协议,如GET /index.html HTTP/1.1) |
$status |
HTTP响应状态码(如200表示成功,404表示未找到,500表示服务器错误) |
$body_bytes_sent |
发送给客户端的响应体字节数(不包括响应头) |
$http_referer |
请求来源页面的URL(用户从哪个页面跳转而来,若直接访问则为-) |
$http_user_agent |
客户端浏览器/设备信息(如Chrome、Firefox、手机APP等) |
$http_x_forwarded_for |
客户端真实IP地址(需代理服务器配合设置,多个IP用逗号分隔) |
若需记录更详细的请求信息(如请求处理时间、上游响应时间、请求长度),可自定义格式:
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
'$status $request_time $upstream_response_time $request_length $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
对应日志样例:
192.168.1.100 - - [02/Oct/2025:10:00:00 +0800] "GET /api/data HTTP/1.1" 200 0.123 0.045 1024 512 "http://example.com/ref" "Mozilla/5.0..." "192.168.1.100, 10.0.0.1"
字段说明:
$request_time:请求处理时间(从读取客户端第一个字节到发送最后一个字节的总时间,单位:秒,含毫秒);$upstream_response_time:向上游服务器(如PHP-FPM、Tomcat)获取响应的时间(多个上游服务器用逗号分隔);$request_length:请求总长度(包括请求行、请求头、请求体,单位:字节)。错误日志默认格式为error,记录服务器运行中的错误信息,格式如下:
error_log /var/log/nginx/error.log error;
对应日志样例:
2025/10/02 10:05:30 [error] 1234#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.100, server: example.com, request: "GET /api/data HTTP/1.1", upstream: "http://10.0.0.1:8080/api/data", host: "example.com"
字段说明:
[error]:错误级别(如error、crit、alert,级别越高越严重);connect() failed (111: Connection refused):错误具体信息(此处为连接上游服务器失败);client: 192.168.1.100:发起请求的客户端IP;server: example.com:虚拟主机名称;request: "GET /api/data HTTP/1.1":触发错误的请求;upstream: "http://10.0.0.1:8080/api/data":上游服务器地址。Nginx主配置文件通常位于/etc/nginx/nginx.conf,虚拟主机配置位于/etc/nginx/conf.d/default.conf(或其他自定义路径)。使用文本编辑器(如vi、nano)打开文件:
sudo vi /etc/nginx/nginx.conf
在http块中添加log_format指令(自定义格式需指定名称,如custom):
http {
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
'$status $request_time $upstream_response_time $request_length $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
# 其他配置...
}
在server块或location块中,使用access_log指令指定日志路径和自定义格式:
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.com.access.log custom; # 使用自定义格式
error_log /var/log/nginx/example.com.error.log error; # 错误日志级别设为error
# 其他配置...
}
保存配置文件后,使用以下命令重新加载Nginx,使配置生效:
sudo nginx -s reload
确保日志目录存在且有正确的写入权限(Nginx进程用户通常为nginx或www-data):
sudo mkdir -p /var/log/nginx
sudo touch /var/log/nginx/example.com.access.log
sudo touch /var/log/nginx/example.com.error.log
sudo chown nginx:nginx /var/log/nginx/*.log
为避免日志文件过大占用磁盘空间,可使用logrotate工具自动轮转日志。创建/etc/logrotate.d/nginx配置文件:
sudo vi /etc/logrotate.d/nginx
添加以下内容(每天轮转、保留14天、压缩旧日志):
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 nginx nginx
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
在应用配置前,使用以下命令测试Nginx配置是否正确:
sudo nginx -t
若输出syntax is ok和configuration file is valid,则表示配置正确。
grep "$(date +%d/%b/%Y)" /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10
awk '{print $1}' /var/log/nginx/access.log | grep "192.168.1.100" | sort | uniq -c | sort -nr
grep " 500 " /var/log/nginx/access.log
sudo tail -n 10 /var/log/nginx/access.log
awk '$7 > 1' /var/log/nginx/access.log
(注:$7为$request_time字段的位置,需根据自定义格式调整)