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.2 access_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.3 log_format配置

語法:
  • log_format name string ……;
默認值: log_format combined "……";
配置段: http
釋義:name表示格式名稱,string表示等義的格式。log_format有一個默認的無需設置的combined日志格式,相當於apache的combined日志格式。
示例1:
  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.4 open_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.5 log_not_found配置

語法:log_not_found on | off;
默認值:log_not_found on;
配置段:http,server,location
作用:是否在error_log中記錄不存在的錯誤,默認是,即記錄。

2.6 log_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.8 error_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
clipboard
釋義:
  • Active connections:當前活躍的連接數。
  • server:表示Nginx啟動到現在共處理了90個連接。
  • accepts:表示Nginx啟動到現在共成功創建90次握手。
  • handled requests:表示總共處理了19次請求。
  • Reading:Nginx讀取到客戶端的 Header 信息數。
  • Writing:Nginx返回給客戶端的 Header 信息數。
  • Waiting:Nginx開啟keep-alive長連接情況下, 既沒有讀也沒有寫, 建立連接情況。
請求丟失數=(握手數-連接數)可以看出,本次狀態顯示沒有丟失請求。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM