Nginx 访问日志配置
Nginx 访问日志配置
访问日志是排查问题和分析流量的重要依据,配置好很有必要。
1. 默认日志位置
# 访问日志
/var/log/nginx/access.log
# 错误日志
/var/log/nginx/error.log
2. 日志格式
Nginx 默认有两种日志格式:
combined(默认)
log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
common
log_format common '$remote_addr [$time_local] "$request" $status $body_bytes_sent';
3. 自定义日志格式
在 nginx.conf 的 http 块添加:
http {
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time';
access_log /var/log/nginx/access.log main;
}
4. 常用日志变量
- $remote_addr: 客户端IP
- $time_local: 本地时间
- $request: 请求行
- $status: 状态码
- $body_bytes_sent: 发送字节数
- $http_referer: 来源页面
- $http_user_agent: 浏览器信息
- $request_time: 请求总耗时
- $upstream_response_time: 后端响应时间
5. 按站点分开日志
不同站点用不同日志文件:
server {
listen 80;
server_name a.com;
access_log /var/log/nginx/a.access.log;
}
server {
listen 80;
server_name b.com;
access_log /var/log/nginx/b.access.log;
}
6. 关闭日志
静态资源不记日志:
location ~* \.(jpg|jpeg|png|gif|css|js)$ {
access_log off;
}
7. 日志切割
Nginx 自带日志切割:
http {
# 每天切割,保留30天
access_log /var/log/nginx/access.log main;
log_not_found off;
}
也可以用 logrotate:
# 配置文件
/etc/logrotate.d/nginx
8. 分析日志
统计访问量
# 统计总请求数
wc -l /var/log/nginx/access.log
# 统计独立IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
统计状态码
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c
统计访问最多的URL
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
总结
Nginx 访问日志配置要点:
- 默认日志在 /var/log/nginx/
- combined 是默认格式
- 自定义格式添加 $request_time 等变量
- 不同站点分开日志文件
- 静态资源可以关闭日志
- 日志要定期切割
- 用 awk 分析日志很方便
做好日志配置,排查问题和分析流量都有用。
Nginx配置访问日志,日志格式详解
前言
网站出问题了,想查一下谁访问过哪个页面,怎么查?Nginx 的访问日志就记录了所有请求。本文讲一下 Nginx 怎么配置访问日志,日志格式都有什么字段。
默认日志位置
Nginx 默认访问日志在:
/var/log/nginx/access.log
错误日志在:
/var/log/nginx/error.log
查看实时日志
tail -f /var/log/nginx/access.log
日志格式定义
Nginx 自带一个默认格式叫 combined:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
常用日志变量
| 变量 | 说明 |
|---|---|
$remote_addr |
客户端 IP |
$time_local |
本地时间 |
$request |
请求行(方法+URL+协议) |
$status |
状态码 |
$body_bytes_sent |
发送字节数 |
$http_referer |
来源页面 |
$http_user_agent |
浏览器 UA |
自定义日志格式
想多记点信息,比如响应时间、X-Forwarded-For:
log_format myformat '$remote_addr - [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct="$upstream_connect_time"';
access_log /var/log/nginx/access.log myformat;
单独给某个站点配日志
多站点的时候,每个站点单独记日志:
server {
listen 80;
server_name www.aaa.com;
root /var/www/aaa;
index index.html;
access_log /var/log/nginx/aaa_access.log;
error_log /var/log/nginx/aaa_error.log;
}
关闭日志
不想记日志(不推荐):
access_log off;
常见坑
坑1:用了 CDN,日志里都是 CDN IP
访问日志里记的都是 CDN 的 IP,不是真实用户的。
原因: 请求经过 CDN 转发了。
解决: 记 X-Forwarded-For:
log_format combined '$http_x_forwarded_for - $remote_user [$time_local] ...';
坑2:日志太大占磁盘
访问日志一直写,把磁盘占满了。
解决: 配 logrotate 自动切割日志。
坑3:查不到某个 IP 的访问记录
想查某个 IP 今天访问了多少次,日志里没有。
原因: 日志已经被切割轮转了。
解决: 看昨天的日志文件:
grep "IP地址" /var/log/nginx/access.log.1
坑4:日志权限不对
Nginx 写不了日志。
解决: 确保日志目录属主是 www:
chown -R www:www /var/log/nginx/
常用日志分析命令
统计访问量前10的IP:
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
统计 404 最多的页面:
awk '($9 == 404)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -nr
总结
一行配置开启日志:
access_log /var/log/nginx/access.log;
记住: 日志是排查问题的第一手资料,出事先看日志。
遇到问题加QQ23979811 协助处理
Nginx配置访问日志,日志切割和分析
前言
Nginx 默认就会记录访问日志,放在 /var/log/nginx/ 目录下。但日志每天都在涨,时间长了占满磁盘,还不好查。本文就讲一下 Nginx 日志配置、切割和分析方法。
默认日志位置
Nginx 默认日志文件:
/var/log/nginx/
├── access.log # 访问日志
└── error.log # 错误日志
日志格式
Nginx 默认的日志格式叫 combined,记录这些信息:
- 客户端 IP
- 访问时间
- 请求方法和路径
- 状态码
- 响应大小
- Referer
- User-Agent
自定义日志格式
在 nginx.conf 的 http 块里可以自定义日志格式:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
}
常用变量
| 变量 | 说明 |
|---|---|
$remote_addr |
客户端 IP |
$time_local |
本地时间 |
$request |
请求行 |
$status |
状态码 |
$body_bytes_sent |
响应大小 |
$http_referer |
来源页面 |
$http_user_agent |
浏览器信息 |
$request_time |
请求耗时 |
按站点分日志
不同站点的日志分开存,方便排查问题:
server {
listen 80;
server_name www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
日志切割
用 logrotate 切割
CentOS7 装完 Nginx 就自动配了 logrotate,不用管。配置文件在:
/etc/logrotate.d/nginx
内容大概是:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
notifempty
create 0640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
意思是:
- 每天切割一次
- 保留 14 天
- 压缩旧日志
- 空日志不切割
手动测试切割
logrotate -f /etc/logrotate.d/nginx
日志分析常用命令
查看访问量前 10 的 IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
查看访问量前 10 的页面
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
查看状态码统计
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
查看爬虫访问
grep 'Googlebot' /var/log/nginx/access.log
查看某个 IP 的访问
grep '192.168.1.1' /var/log/nginx/access.log
常见坑
坑1:日志越来越大占满磁盘
日志不切割,时间长了把磁盘占满。
解决: 确认 logrotate 在运行:
cat /var/lib/logrotate.status
或者手动执行一次:
logrotate -f /etc/logrotate.d/nginx
坑2:日志里 IP 不对
日志里全是 127.0.0.1,不是真实用户 IP。
原因: Nginx 前面还有一层代理(比如 CDN),或者配了反向代理。
解决: 用 $http_x_forwarded_for 代替 $remote_addr:
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" "$http_user_agent"';
坑3:日志级别太高
错误日志里全是 notice,没用还占空间。
解决: 把错误日志级别调高一点:
error_log /var/log/nginx/error.log warn;
可选级别:debug、info、notice、warn、error、crit。
总结
Nginx 日志的常用操作:
| 操作 | 命令 |
|---|---|
| 看访问日志 | tail -f /var/log/nginx/access.log |
| 看错误日志 | tail -f /var/log/nginx/error.log |
| 统计 IP | awk '{print $1}' access.log \| sort \| uniq -c |
| 统计页面 | awk '{print $7}' access.log \| sort \| uniq -c |
| 手动切割 | logrotate -f /etc/logrotate.d/nginx |
日志是排查问题的第一手资料,学会看日志比什么都重要。
遇到问题加QQ23979811 协助处理