Nginx 日誌配置詳情解析

Nginx 日誌配置詳情解析

  在使用nginx進行服務器管理時候,日誌對於統計、審查、排錯來說非常有利。
  nginx日誌相關的配置有:access_log(訪問日誌)、log_format(日誌格式)、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
  nginx有一個非常靈活的日誌記錄模式,每個級別的配置可以有各自獨立的訪問日誌。日誌格式通過log_format命令來定義。(主要基於ngx_http_log_module:用於定義請求日誌格式)

1.access_log配置解析

1.1.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

1.2.access_log默認值

  • access_log log/access.log combined

1.3.access_log配置段

  • http
  • server
  • location
  • if in location
  • limit_except

1.4.access_log語法參數解析

  • gzip:壓縮等級
  • buffer:設置所需內容緩存區大小
  • flush:保存在緩存區的最長時間
  • off:不記錄日誌
  • combined:使用默認的格式記錄日誌(access_log log/access.log combined或access_log log/access.log)

2.log_format配置解析

2.1.log_format語法

  • log_format name string …;

2.2.log_format默認值

  • log_format combined “…”;
    log_format有一個默認的無需設置的combined日誌格式,相當於apache的combined日誌格式。格式如下:
log_format  combined  '$remote_addr - $remote_user  [$time_local]  '
                                   ' "$request"  $status  $body_bytes_sent  '
                                   ' "$http_referer"  "$http_user_agent" ';

2.3.log_format配置段

  • http

2.4.log_format語法參數解析

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

2.5.log_format常用的變量參數:

$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 通用日誌格式下的本地時間。

2.6.log_format使用例子

2.6.1.nginx反向代理時配置log_format

log_format  porxy  '$http_x_forwarded_for - $remote_user  [$time_local]  '
                   ' "$request"  $status $body_bytes_sent '
                   ' "$http_referer"  "$http_user_agent" ';
  • 注:nginx位於負載均衡器,squid,nginx反向代理之後,web服務器無法直接獲取到客戶端真實的IP地址了。 $remote_addr獲取反向代理的IP地址。反向代理服務器在轉發請求的http頭信息中,可以增加X-Forwarded-For信息,用來記錄 客戶端IP地址和客戶端請求的服務器地址。

2.6.2.http發送給客戶端時配置log_format

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;
    }
 }
}

- 注:發送給客戶端的響應頭擁有“sent_http_”前綴。 比如$sent_http_content_range。


3.open_log_file_cache配置解析

3.1.open_log_file_cache語法

  • open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
  • open_log_file_cache off;

3.2.open_log_file_cache默認值

  • open_log_file_cache off;

3.3.open_log_file_cache配置段

  • http
  • server
  • location

3.4.open_log_file_cache語法參數解析

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

3.5.open_log_file_cache的作用

對於每一條日誌記錄,都將是先打開文件,再寫入日誌,然後關閉。可以使用open_log_file_cache來設置日誌文件緩存(默認是off)

3.6.open_log_file_cache使用例子

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

4.log_not_found配置解析

4.1.log_not_found語法

  • log_not_found on | off;

4.2.log_not_found默認值

  • log_not_found on;

4.3.log_not_found配置段

  • http
  • server
  • location

4.4.log_not_found的作用

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


5.log_subrequest配置解析

5.1.log_subrequest語法

  • log_subrequest on | off;

5.2.log_subrequest默認值

  • log_subrequest off;

5.3.log_subrequest配置段

  • http
  • server
  • location

5.4.log_subrequest的作用

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


6.rewrite_log配置解析

6.1.rewrite_log語法

  • rewrite_log on | off;

6.2.rewrite_log默認值

  • rewrite_log off;

6.3.rewrite_log配置段

  • http
  • server
  • location
  • if

6.4.rewrite_log的作用

啓用時將在error log中記錄notice級別的重寫日誌。
基於ngx_http_rewrite_module模塊提供的。用來記錄重寫日誌的。對於調試重寫規則建議開啓。


7.error_log配置解析

7.1.error_log語法

  • error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];

7.2.error_log默認值

  • error_log logs/error.log error;

7.3.error_log配置段

  • http
  • server
  • location
  • main

7.4.error_log的作用

配置錯誤日誌。

發佈了51 篇原創文章 · 獲贊 30 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章