nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx日志配置及状态监控

Nginx日志配置及状态监控方式解读

作者:niechel

文章详细介绍了Nginx的配置技巧,包括日志配置(如access_log、log_format等模块)、状态监控(如stub_status_model编译选项)以及高级配置(如log_not_found、log_subrequest等),帮助用户全面掌握Nginx的日志管理和状态监控方法

一 Nginx请求简介

1.1 请求头部

对于HTTP而言,客户端负责发起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日志相关涉及的配置有:

open_log_file_cache、log_not_found、log_subrequest。

nginx具备非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。

日志格式通过log_format命令来定义。ngx_http_log_module:用于定义请求日志格式。

2.2 access_log配置

语法:

默认值: 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

参数解释:

2.3 log_format配置

语法:

默认值: log_format combined "……";

配置段: http

释义:name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式。

  1 ……
  2     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  3                       '$status $body_bytes_sent "$http_referer" '
  4                       '"$http_user_agent"';
  5 ……
  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 ……

配置相关变量释义:

提示:

2.4 open_log_file_cache配置

语法:

默认值:open_log_file_cache off; #关闭open_log_file_cache

配置段:http,server,location

作用:对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off)。

参数释义:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

2.5 log_not_found配置

2.6 log_subrequest配置

2.7 rewrite_log配置

2.8 error_log配置

三 状态监控

3.1 配置监控

示例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

释义:

请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文