Nginx日志配置及狀態(tài)監(jiān)控方式解讀
一 Nginx請求簡介
1.1 請求頭部
對于HTTP而言,客戶端負責發(fā)起request請求,服務(wù)端負責response響應。
- request:包括請求行、請求頭部、請求數(shù)據(jù);
- response:包括狀態(tài)行、消息報頭、響應正文。
1 [root@nginx ~]# curl -v www.odocker.com 2 * About to connect() to www.odocker.com port 80 (#0) #關(guān)于本次連接信息 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 #請求的服務(wù)端主機 8 > Accept: */* #如上為請求 9 > 10 < HTTP/1.1 200 OK #返回http版本 11 < Server: nginx/1.16.1 #服務(wù)端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> #具體內(nèi)容 21 * Connection #0 to host www.odocker.com left intact
二 日志配置
2.1 日志相關(guān)配置
nginx日志相關(guān)涉及的配置有:
- 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
參數(shù)解釋:
- gzip:壓縮等級。
- buffer:設(shè)置內(nèi)存緩存區(qū)大小。
- flush:保存在緩存區(qū)中的最長時間。
2.3 log_format配置
語法:
- log_format name string ……;
默認值: log_format combined "……";
配置段: http
釋義:name表示格式名稱,string表示等義的格式。log_format有一個默認的無需設(shè)置的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 ……
配置相關(guān)變量釋義:
- $remote_addr:表示客戶端地址;
- $remote_user:表示http客戶端請求Nginx認證的用戶名;
- $time_local:Nginx通用日志格式下的本地時間;
- $request:request請求行,請求的URL、GET等方法、HTTP協(xié)議版本;
- $request_length:請求的長度;
- $request_time:請求處理時間,單位為秒,精度為毫秒;
- $status:response返回狀態(tài)碼;
- $body_bytes_sent:發(fā)送給客戶端的字節(jié)數(shù),不包括響應頭的大小,即服務(wù)端響應給客戶端body信息大??;
- $http_referer:http上一級頁面,即從哪個頁面鏈接訪問過來的,用于防盜鏈、用戶行為分析;
- $http_user_agent:http頭部信息,記錄客戶端瀏覽器相關(guān)信息;
- $connection:連接的序列號;
- $connection_requesta:當前通常一個連接獲得的請求數(shù)量;
- $msec:日志寫入時間,單位為秒,精度為毫秒;
- $pipe:如果請求是通過HTTP流水線(pipelined)發(fā)送,pipe值為‘p’,否則為“.”;
- $http_x_forwarded_for:http請求攜帶的http信息。
提示:
- 如果nginx位于負載均衡器,squid,nginx反向代理之后,web服務(wù)器無法直接獲取到客戶端真實的IP地址了。
- $remote_addr獲取反向代理的IP地址。
- 反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中,可以增加X-Forwarded-For信息,用來記錄客戶端IP地址和客戶端請求的服務(wù)器地址。
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; #關(guān)閉open_log_file_cache
配置段:http,server,location
作用:對于每一條日志記錄,都將是先打開文件,再寫入日志,然后關(guān)閉??梢允褂胦pen_log_file_cache來設(shè)置日志文件緩存(默認是off)。
參數(shù)釋義:
- max:設(shè)置緩存中的最大文件描述符數(shù)量,如果緩存被占滿,采用LRU算法將描述符關(guān)閉。
- inactive:設(shè)置存活時間,默認是10s。
- min_uses:設(shè)置在inactive時間段內(nèi),日志文件最少使用多少次后,該日志文件描述符記入緩存中,默認是1次。
- valid:設(shè)置檢查頻率,默認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模塊提供的。用來記錄重寫日志的,對于調(diào)試重寫規(guī)則建議開啟。啟用時將在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
- 作用:配置錯誤日志。
三 狀態(tài)監(jiān)控
3.1 配置監(jiān)控
- Nginx狀態(tài)監(jiān)控使用--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:當前活躍的連接數(shù)。
- server:表示Nginx啟動到現(xiàn)在共處理了90個連接。
- accepts:表示Nginx啟動到現(xiàn)在共成功創(chuàng)建90次握手。
- handled requests:表示總共處理了19次請求。
- Reading:Nginx讀取到客戶端的 Header 信息數(shù)。
- Writing:Nginx返回給客戶端的 Header 信息數(shù)。
- Waiting:Nginx開啟keep-alive長連接情況下, 既沒有讀也沒有寫, 建立連接情況。
請求丟失數(shù)=(握手數(shù)-連接數(shù))可以看出,本次狀態(tài)顯示沒有丟失請求。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用nginx正向代理實現(xiàn)內(nèi)網(wǎng)域名轉(zhuǎn)發(fā)過程解析
這篇文章主要介紹了使用nginx正向代理實現(xiàn)內(nèi)網(wǎng)域名轉(zhuǎn)發(fā)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08

