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

寫在前面

日誌對於統計排錯來講很是有利的。本文總結了 Nginx 日誌相關的配置如 access_log、 log_format、open_log_file_cache、 log_not_found、 log_subrequest、 rewrite_log、 error_log。nginx

配置Nginx日誌

Nginx 有一個很是靈活的日誌記錄模式。每一個級別的配置能夠有各自獨立的訪問日誌。日誌格式經過 log_format命令來定義。 ngx_http_log_module 是用來定義請求日誌格式的。web

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;

配置段:apache

http, server, location, if in location, limit_except
  • gzip 壓縮等級。
  • buffer 設置內存緩存區大小。
  • flush 保存在緩存區中的最長時間

不記錄日誌:瀏覽器

access_log off;

使用默認 combined 格式記錄日誌:緩存

access_log logs/access.log

或者bash

access_log logs/access.log combined;

log_format 指令

語法:服務器

log_format name string …;

默認值:微信

log_format combined 「…」 ;

配置段:

http
  • name 表示格式名稱
  • string 表示等義的格式。

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" ';

日誌格式容許包含的變量註釋以下:

  • $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。

實例以下:

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 指令

語法:

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)。

參數註釋以下:

  • max:設置緩存中的最大文件描述符數量,若是緩存被佔滿,採用 LRU 算法將描述符關閉。
  • inactive:設置存活時間,默認是 10s
  • min_uses:設置在 inactive 時間段內,日誌文件最少使用多少次後,該日誌文件描述符記入緩存中,默認是 1 次
  • valid:設置檢查頻率,默認 60s
  • off:禁用緩存

實例以下:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

log_not_found 指令

語法:

log_not_found on | off;

默認值:

log_not_found on;

配置段:

http, server, location

是否在 error_log 中記錄不存在的錯誤。默認是。

log_subrequest 指令

語法:

log_subrequest on | off;

默認值:

log_subrequest off;

配置段:

http, server, location

是否在 access_log 中記錄子請求的訪問日誌。默認不記錄。

rewrite_log 指令

由 ngx_http_rewrite_module 模塊提供的。用來記錄重寫日誌的。對於調試重寫規則建議開啓。 Nginx 重寫規則指南

語法:

rewrite_log on | off;

默認值:

rewrite_log off;

配置段:

http, server, location, if

啓用時將在 error log 中記錄 notice 級別的重寫日誌。

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

配置錯誤日誌。

好了,今天就聊到這兒吧!別忘了點個贊,給個在看和轉發,讓更多的人看到,一塊兒學習,一塊兒進步!!

寫在最後

若是你以爲冰河寫的還不錯,請微信搜索並關注「 冰河技術 」微信公衆號,跟冰河學習高併發、分佈式、微服務、大數據、互聯網和雲原生技術,「 冰河技術 」微信公衆號更新了大量技術專題,每一篇技術文章乾貨滿滿!很多讀者已經經過閱讀「 冰河技術 」微信公衆號文章,吊打面試官,成功跳槽到大廠;也有很多讀者實現了技術上的飛躍,成爲公司的技術骨幹!若是你也想像他們同樣提高本身的能力,實現技術能力的飛躍,進大廠,升職加薪,那就關注「 冰河技術 」微信公衆號吧,天天更新超硬核技術乾貨,讓你對如何提高技術能力再也不迷茫!

相關文章
相關標籤/搜索