Nginx 配置 access log 請求日誌詳解

配置示例

log_format main '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $bytes_sent '
                '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

log_format 指令

默認值:log_format combined "...";html

標籤段位置:httpnginx

經常使用變量:瀏覽器

  • $bytes_sent 發送給客戶端內容的字節數,位於ngx_http_log_module
  • $request_length 請求長度(包括請求行,標頭和請求正文),位於ngx_http_log_module
  • $request_time 要求以毫秒爲單位的處理時間,位於ngx_http_log_module
  • $status 狀態碼,位於ngx_http_log_module
  • $time_local 請求的本地時間和市區,位於ngx_http_log_module
  • $remote_addr 客戶端地址,位於ngx_http_core_module
  • $remote_user Basic受權模式下的客戶端用戶名稱,位於ngx_http_core_module
  • $request 完整的請求地址和協議,位於ngx_http_core_module
  • $request_body 請求的body內容,位於ngx_http_core_module
  • $http_host 域名或IP
  • $http_referer 請求的跳轉來源
  • $http_user_agent 瀏覽器信息
  • $http_x_forwarded_for 記錄客戶端地址的配置
若是變量對應的值不存在,則以「 -」代替。

access_log 指令

默認值:access_log logs/access.log combined;日誌

標籤段位置:http, server, location, if in location, limit_exceptcode

access_log off;
access_log logs/access.log main;
access_log logs/access.log main buffer=32k flush=5s;
map $status $loggable {
    ~^[23]  0;
    default 1;
}

# 非2xx 和 3xx 的狀態才寫入日誌
access_log /path/to/access.log main if=$loggable;

重載配置

../sbin/nginx -t
../sbin/nginx -s reload

參考:http://nginx.org/en/docs/http...orm

http://nginx.org/en/docs/http...server

相關文章
相關標籤/搜索