1. 程式人生 > >【Nginx】如何配置Nginx日誌?這是最全面的一篇了!!

【Nginx】如何配置Nginx日誌?這是最全面的一篇了!!

## 寫在前面 > 日誌對於統計排錯來說非常有利的。本文總結了 Nginx 日誌相關的配置如 access_log、 log_format、open_log_file_cache、 log_not_found、 log_subrequest、 rewrite_log、 error_log。 ## 配置Nginx日誌 Nginx 有一個非常靈活的日誌記錄模式。每個級別的配置可以有各自獨立的訪問日誌。日誌格式通過 log_format命令來定義。 ngx_http_log_module 是用來定義請求日誌格式的。 ## access_log 指令 **語法:** ```bash 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; ``` **預設值:** ```bash access_log logs/access.log combined; ``` **配置段:** ```bash http, server, location, if in location, limit_except ``` * gzip 壓縮等級。 * buffer 設定記憶體快取區大小。 * flush 儲存在快取區中的最長時間 **不記錄日誌:** ```bash access_log off; ``` **使用預設 combined 格式記錄日誌:** ```bash access_log logs/access.log ``` 或者 ```bash access_log logs/access.log combined; ``` ## log_format 指令 **語法:** ```bash log_format name string …; ``` **預設值:** ```bash log_format combined “…” ; ``` **配置段:** ```bash http ``` * name 表示格式名稱 * string 表示等義的格式。 log_format 有一個預設的無需設定的 combined 日誌格式,相當於apache 的 combined 日誌格式,如下所示: ```bash log_format combined '$remote_addr - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" '; ``` 如果 nginx 位於負載均衡器, squid, nginx 反向代理之後, web 伺服器無法直接獲取到客戶端真實的 IP 地址了。 $remote_addr 獲取反向代理的 IP 地址。反向代理伺服器在轉發請求的 http 頭資訊中,可以增加 X-ForwardedFor 資訊,用來記錄 客戶端 IP 地址和客戶端請求的伺服器地址。 如下所示: ```bash log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" '; ``` 日誌格式允許包含的變數註釋如下: * $remote_addr, $http_x_forwarded_for 記錄客戶端 IP 地址 * $remote_user 記錄客戶端使用者名稱稱 * $request 記錄請求的 URL 和 HTTP 協議 * $status 記錄請求狀態 * $body_bytes_sent 傳送給客戶端的位元組數,不包括響應頭的大小; 該變數與 Apache 模組 mod_log_config 裡的“%B”引數相容。 * $bytes_sent 傳送給客戶端的總位元組數。 * $connection 連線的序列號。 * $connection_requests 當前通過一個連接獲得的請求數量。 * $msec 日誌寫入時間。單位為秒,精度是毫秒。 * $pipe 如果請求是通過 HTTP 流水線(pipelined)傳送, pipe 值為“p”,否則為“.”。 * $http_referer 記錄從哪個頁面連結訪問過來的 * $http_user_agent 記錄客戶端瀏覽器相關資訊 * $request_length 請求的長度(包括請求行,請求頭和請求正文)。 * $request_time 請求處理時間,單位為秒,精度毫秒; 從讀入客戶端的第一個位元組開始,直到把最後一個字元傳送給客戶端後進行日誌寫入為止。 * $time_iso8601 ISO8601 標準格式下的本地時間。 * $time_local 通用日誌格式下的本地時間。 **注意:傳送給客戶端的響應頭擁有“sent_http_”字首。 比如$sent_http_content_range。** **例項如下:** ```bash http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$gzip_ratio" $request_time $bytes_sent $request_length'; log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" ' '"$status" $body_bytes_sent $request_time $bytes_sent $request_length ' '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]'; open_log_file_cache max=1000 inactive=60s; server { server_name ~^(www\.)?(.+)$; access_log logs/$2-access.log main; error_log logs/$2-error.log; location /srcache { access_log logs/access-srcache.log srcache_log; } } } ``` ## open_log_file_cache 指令 **語法:** ```bash open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; ``` **預設值:** ```bash open_log_file_cache off; ``` **配置段:** ```bash http, server, location ``` 對於每一條日誌記錄,都將是先開啟檔案,再寫入日誌,然後關閉。可以使用 open_log_file_cache 來設定日誌檔案快取(預設是 off)。 **引數註釋如下:** * max:設定快取中的最大檔案描述符數量,如果快取被佔滿,採用 LRU 演算法將描述符關閉。 * inactive:設定存活時間,預設是 10s * min_uses:設定在 inactive 時間段內,日誌檔案最少使用多少次後,該日誌檔案描述符記入快取中,預設是 1 次 * valid:設定檢查頻率,預設 60s * off:禁用快取 **例項如下:** ```bash open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2; ``` ## log_not_found 指令 **語法:** ```bash log_not_found on | off; ``` **預設值:** ```bash log_not_found on; ``` **配置段:** ```bash http, server, location ``` 是否在 error_log 中記錄不存在的錯誤。預設是。 ## log_subrequest 指令 **語法:** ```bash log_subrequest on | off; ``` **預設值:** ```bash log_subrequest off; ``` **配置段:** ```bash http, server, location ``` 是否在 access_log 中記錄子請求的訪問日誌。預設不記錄。 ## rewrite_log 指令 由 ngx_http_rewrite_module 模組提供的。用來記錄重寫日誌的。對於除錯重寫規則建議開啟。 Nginx 重寫規則指南 **語法:** ```bash rewrite_log on | off; ``` **預設值:** ```bash rewrite_log off; ``` **配置段:** ```bash http, server, location, if ``` 啟用時將在 error log 中記錄 notice 級別的重寫日誌。 ## error_log 指令 **語法:** ```bash error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg]; ``` **預設值:** ```bash error_log logs/error.log error; ``` **配置段:** ```bash main, http, server, location ``` 配置錯誤日誌。 **好了,今天就聊到這兒吧!別忘了點個贊,給個在看和轉發,讓更多的人看到,一起學習,一起進步!!** ## 寫在最後 > 如果你覺得冰河寫的還不錯,請微信搜尋並關注「 **冰河技術** 」微信公眾號,跟冰河學習高併發、分散式、微服務、大資料、網際網路和雲原生技術,「 **冰河技術** 」微信公眾號更新了大量技術專題,每一篇技術文章乾貨滿滿!不少讀者已經通過閱讀「 **冰河技術** 」微信公眾號文章,吊打面試官,成功跳槽到大廠;也有不少讀者實現了技術上的飛躍,成為公司的技術骨幹!如果你也想像他們一樣提升自己的能力,實現技術能力的飛躍,進大廠,升職加薪,那就關注「 **冰河技術** 」微信公眾號吧,每天更新超硬核技術乾貨,讓你對如何提升技術能力不再迷茫! ![](https://img-blog.csdnimg.cn/202007091317208