nginx-md-diange/nginx-md/第六章:Nginx日志.md

261 lines
9.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>Nginx日志管理</center></h1>
作者:行癫(盗版必究)
------
## 一:日志介绍
#### 1.介绍
nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志, 所需日志模块 ngx_http_log_module 的支持
日志格式通过 log_format 命令来定义
日志对于统计和排错是非常有利的
nginx 日志相关的配置 包括 access_log、log_format、open_log_file_cache、rewrite_log、error_log
#### 2.总结
Nginx中通过access_log和error_log指令配置访问日志和错误日志通过log_format我们可以自定义日志格式。如果日志文件路径中使用了变量我们可以通过open_log_file_cache 指令来设置缓存提升性能。其他的根据自己的使用场景定义。详细的日志配置信息可以参考Nginx官方文档
## 二:访问日志
#### 1.设置访问日志
```shell
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
```
path 指定日志的存放位置
format 指定日志的格式。默认使用预定义的combined
buffer 用来指定日志写入时的缓存大小。默认是64k
gzip 日志写入前先进行压缩。压缩率可以指定从1到9数值越大压缩比越高同时压缩的速度也越慢。默认是1
flush 设置缓存的有效时间。如果超过flush指定的时间缓存中的内容将被清空
if 条件判断。如果指定的条件计算为0或空字符串那么该请求不会写入日志
#### 2.关闭访问日志
```shell
access_log off;
```
#### 3.应用范围
可以应用access_log指令的作用域分别有httpserverlocation等。也就是说在这几个作用域外使用该指令Nginx会报错
指定日志的写入路径为/var/logs/nginx-access.log日志格式使用默认的combined
```shell
access_log /var/logs/nginx-access.log
```
指定日志的写入路径为/var/logs/nginx-access.log日志格式使用默认的combined指定日志的缓存大小为 32k日志写入前启用 gzip 进行压缩,压缩比使用默认值 1缓存数据有效时间为1分钟
```shell
access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m
```
#### 4.log_format 指令
Nginx 预定义了名为 combined 日志格式,如果没有明确指定日志格式默认使用该格式
如果不想使用Nginx预定义的格式可以通过log_format指令来自定义
```shell
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
```
语法:
```shelll
log_format name [escape=default|json] string ...;
```
name 格式名称。在 access_log 指令中引用
escape 设置变量中的字符编码方式是json还是default默认是default
string 要定义的日志格式内容。该参数可以有多个参数中可以使用Nginx变量
自定义日志格式的使用:
```shell
access_log /var/logs/nginx-access.log main
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
```
使用log_format指令定义了一个main的格式并在access_log指令中引用了它
假如客户端有发起请求https://qf.com/,我们看一下我截取的一个请求的日志记录
```shell
10.0.105.207 - - [01/Jul/2019:10:44:36 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "-"
我们看到最终的日志记录中$remote_user$http_referer$http_x_forwarded_for都对应了一个-,这是因为这几个变量为空。
```
## 三:错误日志
#### 1.简介
错误日志在Nginx中是通过error_log指令实现的
该指令记录服务器和请求处理过程中的错误信息
#### 2.语法
配置错误日志文件的路径和日志级别
```shell
error_log file [level];
Default:
error_log logs/error.log error;
```
file 参数指定日志的写入位置
level 参数指定日志的级别
注意:
level可以是debug, info, notice, warn, error, crit, alert,emerg中的任意值。可以看到其取值范围是按紧急程度从低到高排列的。只有日志的错误级别等于或高于level指定的值才会写入错误日志中。默认值是error
#### 3.基本用法
```shell
error_log /var/logs/nginx/nginx-error.log
```
指定了错误日志的路径为:/var/logs/nginx/nginx-error.log日志级别使用默认的 error
#### 4.作用域
main http, mail, stream, server, location
## 四:日志文件描述符缓存
#### 1.简介
每一条日志记录的写入都是先打开文件再写入记录,然后关闭日志文件
如果你的日志文件路径中使用了变量,如 access_log /var/logs/$host/nginx-access.log
为提高性能可以使用open_log_file_cache指令设置日志文件描述符的缓存
#### 2.语法
```shell
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
```
max 设置缓存中最多容纳的文件描述符数量如果被占满采用LRU算法将描述符关闭
inactive 设置缓存存活时间默认是10s
min_uses 在inactive时间段内日志文件最少使用几次该日志文件描述符记入缓存默认是1次
valid设置多久对日志文件名进行检查看是否发生变化默认是60s
off不使用缓存。默认为off
#### 3.作用域
http、server、location
#### 4.使用案例
```shell
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
```
设置缓存最多缓存1000个日志文件描述符20s内如果缓存中的日志文件描述符至少被被访问2次才不会被缓存关闭。每隔1分钟检查缓存中的文件描述符的文件名是否还存在
## 五:地址重写日志
#### 1.简介
由ngx_http_rewrite_module模块提供的
用来记录重写日志的
将在error log中记录notice级别的重写日志
#### 2.基本语法
```shell
rewrite_log on | off;
```
默认值off
#### 3.作用域
http, server, location, if
## 六Nginx日志轮转
#### 1.轮转文件
```shell
[root@192 ~]# vim /etc/logrotate.d/nginx
/var/log/nginx/*.log { #指定需要轮转处理的日志文件
daily #日志文件轮转周期,可用值为: daily/weekly/yearly
missingok # 忽略错误信息
rotate 7 # 轮转次数即最多存储7个归档日志会删除最久的归档日志
minsize 5M #限制条件大于5M的日志文件才进行分割否则不操作
dateext # 以当前日期作为命名格式
compress # 轮循结束后已归档日志使用gzip进行压缩
delaycompress # 与compress共用最近的一次归档不要压缩
notifempty # 日志文件为空,轮循不会继续执行
create 640 nginx nginx #新日志文件的权限
sharedscripts #有多个日志需要轮询时,只执行一次脚本
postrotate # 将日志文件转储后执行的命令。以endscript结尾命令需要单独成行
if [ -f /var/run/nginx.pid ]; then #判断nginx的PID。# 默认logrotate会以root身份运行
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
执行命令:
[root@192 nginx]# /usr/sbin/logrotate -f /etc/logrotate.conf
创建计划任务:
[root@192 nginx]# crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.conf
```
注意:
USR1停止接受新的连接等待当前连接停止重新载入配置文件重新打开日志文件重启服务器实现相对平滑的不关机的更改
## 七:面试案例
#### 1.简介
nginx 的 access log 中可以记录很多有价值的信息,通过分析 access log可以收集到很多指标
PVPV(访问量) 即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次。
UVUV(独立访客)即Unique Visitor,访问您网站的一台电脑客户端为一个访客。00:00-24:00内相同的客户端只被计算一次
#### 2.案例
```shell
1.根据访问IP统计UV
awk '{print $1}' access.log|sort | uniq -c |wc -l
2.统计访问URL统计PV
awk '{print $7}' access.log|wc -l
3.查询访问最频繁的URL
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more
4.查询访问最频繁的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more
5.查询访问最频繁的前10的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 10
```