1. 程式人生 > 實用技巧 >Nginx日誌配置及狀態監控

Nginx日誌配置及狀態監控

一 Nginx請求簡介

1.1 請求頭部

對於HTTP而言,客戶端負責發起request請求,服務端負責response響應。 request:包括請求行、請求頭部、請求資料; response:包括狀態行、訊息報頭、響應正文。
[root@master conf.d]# curl -v 192.168.1.220
* About to connect() to 192.168.1.220 port 80 (#0)  ##關於本次連線資訊
*   Trying 192.168.1.220...
* Connected to 192.168.1.220 (192.168.1.220) port 80 (#0)
> GET / HTTP/1.1 #HTTP版本 > User-Agent: curl/7.29.0 #客戶端資訊 > Host: 192.168.1.220 #請求的服務端主機 > Accept: */* #如上為請求 > < HTTP/1.1 200 OK #返回http版本 < Server: nginx/1.18.0 #服務端Web型別 < Date: Sun, 22 Nov 2020 09:40:16 GMT #日期時間 < Content-Type: text/html #返回的型別 < Content-Length: 612 #長度 < Last-Modified: Thu, 29 Oct 2020 15:25:17 GMT #日期時間 < Connection: keep-alive #長連線 < ETag: "5f9adedd-264" #Etag < Accept-Ranges: bytes #大小單位 < <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> #具體內容 <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="
http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> * Connection #0 to host 192.168.1.220 left intact [root@master conf.d]#

二 日誌配置

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:
[root@master conf.d]# cat status.conf 
server {
    server_name  192.168.1.220;

    error_page  404 403 500 502 503 504  /error.html;
    location = /error.html {
        root    /usr/share/nginx/html;
    }

    location / {
        root    /usr/share/nginx/blog;
        index   index.html;
    }
    location /ok {
        alias   /usr/share/nginx/yes;
        index   index.html;
    }
    location /mystatus {
        stub_status on;
        access_log off;
    }
}
[root@master conf.d]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@master conf.d]# nginx -s reload
[root@master conf.d]# curl http://192.168.1.220/mystatus
Active connections: 1 
server accepts handled requests
 9 9 7 
Reading: 0 Writing: 1 Waiting: 0 
[root@master conf.d]# curl http://192.168.1.220/mystatus
Active connections: 1 
server accepts handled requests
 10 10 8 
Reading: 0 Writing: 1 Waiting: 0 
[root@master conf.d]# curl http://192.168.1.220/mystatus
Active connections: 1 
server accepts handled requests
 11 11 9 
Reading: 0 Writing: 1 Waiting: 0 
[root@master conf.d]# curl http://192.168.1.220/mystatus
Active connections: 1 
server accepts handled requests
 12 12 10 
Reading: 0 Writing: 1 Waiting: 0 
[root@master conf.d]# 
釋義:
  • Active connections:當前活躍的連線數。
  • server:表示Nginx啟動到現在共處理了90個連線。
  • accepts:表示Nginx啟動到現在共成功建立90次握手。
  • handled requests:表示總共處理了19次請求。
  • Reading:Nginx讀取到客戶端的 Header 資訊數。
  • Writing:Nginx返回給客戶端的 Header 資訊數。
  • Waiting:Nginx開啟keep-alive長連線情況下, 既沒有讀也沒有寫, 建立連線情況。
請求丟失數=(握手數-連線數)可以看出,本次狀態顯示沒有丟失請求。