日誌對於統計排錯來講很是有利的。本文總結了 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 是用來定義請求日誌格式的。web
語法:面試
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;
配置段:apache
http, server, location, if in location, limit_except
不記錄日誌:瀏覽器
access_log off;
使用默認 combined 格式記錄日誌:緩存
access_log logs/access.log
或者bash
access_log logs/access.log combined;
語法:服務器
log_format name string …;
默認值:微信
log_format combined 「…」 ;
配置段:
http
log_format 有一個默認的無需設置的 combined 日誌格式,至關於apache 的 combined 日誌格式,以下所示:
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 地址和客戶端請求的服務器地址。 以下所示:
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" ';
日誌格式容許包含的變量註釋以下:
注意:發送給客戶端的響應頭擁有「sent_http_」前綴。 好比$sent_http_content_range。
實例以下:
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 max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off;
默認值:
open_log_file_cache off;
配置段:
http, server, location
對於每一條日誌記錄,都將是先打開文件,再寫入日誌,而後關閉。可使用 open_log_file_cache 來設置日誌文件緩存(默認是 off)。
參數註釋以下:
實例以下:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
語法:
log_not_found on | off;
默認值:
log_not_found on;
配置段:
http, server, location
是否在 error_log 中記錄不存在的錯誤。默認是。
語法:
log_subrequest on | off;
默認值:
log_subrequest off;
配置段:
http, server, location
是否在 access_log 中記錄子請求的訪問日誌。默認不記錄。
由 ngx_http_rewrite_module 模塊提供的。用來記錄重寫日誌的。對於調試重寫規則建議開啓。 Nginx 重寫規則指南
語法:
rewrite_log on | off;
默認值:
rewrite_log off;
配置段:
http, server, location, if
啓用時將在 error log 中記錄 notice 級別的重寫日誌。
語法:
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
配置錯誤日誌。
好了,今天就聊到這兒吧!別忘了點個贊,給個在看和轉發,讓更多的人看到,一塊兒學習,一塊兒進步!!
若是你以爲冰河寫的還不錯,請微信搜索並關注「 冰河技術 」微信公衆號,跟冰河學習高併發、分佈式、微服務、大數據、互聯網和雲原生技術,「 冰河技術 」微信公衆號更新了大量技術專題,每一篇技術文章乾貨滿滿!很多讀者已經經過閱讀「 冰河技術 」微信公衆號文章,吊打面試官,成功跳槽到大廠;也有很多讀者實現了技術上的飛躍,成爲公司的技術骨幹!若是你也想像他們同樣提高本身的能力,實現技術能力的飛躍,進大廠,升職加薪,那就關注「 冰河技術 」微信公衆號吧,天天更新超硬核技術乾貨,讓你對如何提高技術能力再也不迷茫!