
正文
004.Nginx日志配置及状态监控
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一Nginx请求简介1.1请求头部
对于HTTP而言,客户端负责发起request请求,服务端负责response响应。request:包括请求行、请求头部、请求数据;response:包括状态行、消息报头、响应正文。
1 [root@nginx ~]# curl -v www.odocker.com
2 * About to connect() to www.odocker.com port 80 (#0)#关于本次连接信息
3 * Trying 113.31.119.149...
4 * Connected to www.odocker.com (113.31.119.149) port 80 (#0)
5 > GET / HTTP/1.1#HTTP版本
6 > User-Agent: curl/7.29.0#客户端信息
7 > Host: www.odocker.com#请求的服务端主机
8 > Accept: */*#如上为请求
9 >
10 < HTTP/1.1 200 OK#返回http版本
11 < Server: nginx/1.16.1#服务端Web类型
12 < Date: Fri, 06 Mar 2020 13:09:40 GMT#日期时间
13 < Content-Type: text/html#返回的类型
14 < Content-Length: 13#长度
15 < Last-Modified: Thu, 05 Mar 2020 11:12:26 GMT#日期时间
16 < Connection: keep-alive#长连接
17 < ETag: "5e60de9a-d"#Etag
18 < Accept-Ranges: bytes#大小单位
19 <
20 <h1>www</h1>#具体内容
21 * Connection #0 to host www.odocker.com left intact
二日志配置2.1日志相关配置
nginx日志相关涉及的配置有:access_log:访问日志;log_format:日志格式;rewrite_log:重定向日志;error_log:错误日志;open_log_file_cache、log_not_found、log_subrequest。nginx具备非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令来定义。ngx_http_log_module:用于定义请求日志格式。2.2access_log配置
语法: - access_log path [format [buffer=size [flush=time]]];
- access_log path format gzip[=level] [buffer=size] [flush=time];
- access_log syslog:server=address[,parameter=value] [format];
- access_log off;#不记录日志
默认值: access_log logs/access.log combined;使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;配置段: http, server, location, if in location, limit_except参数解释:- gzip:压缩等级。
- buffer:设置内存缓存区大小。
- flush:保存在缓存区中的最长时间。
2.3log_format配置
语法: - log_format name string ……;
默认值: log_format combined "……";配置段: http释义:name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式。示例1:
- access_log path [format [buffer=size [flush=time]]];
- access_log path format gzip[=level] [buffer=size] [flush=time];
- access_log syslog:server=address[,parameter=value] [format];
- access_log off;#不记录日志
- gzip:压缩等级。
- buffer:设置内存缓存区大小。
- flush:保存在缓存区中的最长时间。
- log_format name string ……;
1 ……
2 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
3 '$status $body_bytes_sent "$http_referer" '
4 '"$http_user_agent"';
5 ……
示例2:
1 ……
2 log_format proxy '$remote_addr - $remote_user [$time_local] "$request" '
3 '$status $body_bytes_sent "$http_referer" '
4 '"$http_user_agent" "$http_user_agent" ';
5 ……
配置相关变量释义:
$remote_addr:表示客户端地址;
$remote_user:表示http客户端请求Nginx认证的用户名;
$time_local:Nginx通用日志格式下的本地时间;
$request:request请求行,请求的URL、GET等方法、HTTP协议版本;
$request_length:请求的长度;
$request_time:请求处理时间,单位为秒,精度为毫秒;
$status:response返回状态码;
$body_bytes_sent:发送给客户端的字节数,不包括响应头的大小,即服务端响应给客户端body信息大小;
$http_referer:http上一级页面,即从哪个页面链接访问过来的,用于防盗链、用户行为分析;
$http_user_agent:http头部信息,记录客户端浏览器相关信息;
$connection:连接的序列号;
$connection_requesta:当前通常一个连接获得的请求数量;
$msec:日志写入时间,单位为秒,精度为毫秒;
$pipe:如果请求是通过HTTP流水线(pipelined)发送,pipe值为‘p’,否则为“.”;
$http_x_forwarded_for:http请求携带的http信息。
提示:如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。 $remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
2.4open_log_file_cache配置
语法:
- open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
- open_log_file_cache off;
默认值:open_log_file_cache off;#关闭open_log_file_cache
配置段:http,server,location
作用:对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off)。
参数释义:
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s。
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次。
valid:设置检查频率,默认60s。
off:禁用缓存。
示例1:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
2.5log_not_found配置
语法:log_not_found on | off;
默认值:log_not_found on;
配置段:http,server,location
作用:是否在error_log中记录不存在的错误,默认是,即记录。
2.6log_subrequest配置
语法:log_subrequest on | off;
默认值:log_subrequest off;
配置段:http,server,location
作用:是否在access_log中记录子请求的访问日志,默认否,即不记录。
2.7 rewrite_log配置
语法: rewrite_log on | off;
默认值:rewrite_log off;
配置段:http,server,location,if
作用:由ngx_http_rewrite_module模块提供的。用来记录重写日志的,对于调试重写规则建议开启。启用时将在error log中记录notice级别的重写日志。
2.8error_log配置
语法:error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默认值:error_log logs/error.log error;
配置段:main,http,server,location
作用:配置错误日志。
三状态监控3.1配置监控
Nginx状态监控使用--with-http_stub_status_modele编译模块,语法:语法:stub_status on | off;默认值:stub_status off;配置段:server,location示例01:
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/status.conf
2 server {
3 server_name status.linuxds.com;
4
5 error_page 404 403 500 502 503 504 /error.html;
6 location = /error.html {
7 root /usr/share/nginx/html;
8 }
9
10 location / {
11 root /usr/share/nginx/blog;
12 index index.html;
13 }
14 location /ok {
15 alias /usr/share/nginx/yes;
16 index index.html;
17 }
18 location /mystatus {
19 stub_status on;
20 access_log off;
21 }
22 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf
2 [root@nginx01 ~]# nginx -s reload
浏览器访问:http://status.linuxds.com/mystatus

释义:
- Active connections:当前活跃的连接数。
- server:表示Nginx启动到现在共处理了90个连接。
- accepts:表示Nginx启动到现在共成功创建90次握手。
- handled requests:表示总共处理了19次请求。
- Reading:Nginx读取到客户端的 Header 信息数。
- Writing:Nginx返回给客户端的 Header 信息数。
- Waiting:Nginx开启keep-alive长连接情况下, 既没有读也没有写, 建立连接情况。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。








