1. 程式人生 > >Nginx Access Log 設定訪問日誌,過濾日誌中成功的請求不輸出到日誌中

Nginx Access Log 設定訪問日誌,過濾日誌中成功的請求不輸出到日誌中

Nginx處理請求後把關於客戶端請求的資訊寫到訪問日誌。預設,訪問日誌位於 logs/access.log,寫到日誌的資訊是預定義的、組合的格式。要覆蓋預設的配置,使用log_format指令來配置一個記錄資訊的格式,同樣使用access_log 指令到設定日誌和格式和位置。格式定義使用變數。

1、自定義一個日誌格式

log_format  mylogformat '"$remote_addr" "[$time_local]" "$request_method" '                       '"$uri" "$request_uri" "$request_time" "$status" "$body_bytes_sent"'                        '"$http_referer" "$http_x_forwarded_for" "$http_user_agent" "$upstream_status"'                       '"$upstream_addr" "$upstream_response_time"';

2、然後配置新的生成日誌檔名:

access_log  logs/access1.log  mylogformat if=$loggable;

3、過濾掉需要過濾的日誌,此處是過濾掉以2或者3開頭的響應碼

map $status $loggable {                 ~^[23]  0;                      default 1;   }  

具體配置如下圖:

測試如下:

日誌中不會輸出200的錯誤