nginx access日志配置
access_log日志配置
access_log用來定義日志級別,日志位置。語法如下:
日志級別: debug > info > notice > warn > error > crit > alert > emerg
語法格式: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
默認值 : access_log logs/access.log combined;
作用域 : http, server, location, if in location, limit_except
實例一:
access_log /spool/logs/nginx-access.log compression buffer=32k;
log_format 定義日志格式
語法格式: log_format name [escape=default|json] string ...;
默認值 : log_format combined "...";
作用域 : http
實例一:
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /spool/logs/nginx-access.log compression buffer=32k;
常見的日志變量
$remote_addr
,$http_x_forwarded_for
記錄客戶端IP地址$remote_user
記錄客戶端用戶名稱$request
記錄請求的URL和HTTP協議(GET,POST,DEL,等)$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
通用日志格式下的本地時間。
open_log_file_cache
使用open_log_file_cache來設置日志文件緩存(默認是off)。
- max:設置緩存中的最大文件描述符數量,如果緩存被占滿,采用LRU算法將描述符關閉。
- inactive:設置存活時間,默認是10s
- min_uses:設置在inactive時間段內,日志文件最少使用多少次后,該日志文件描述符記入緩存中,默認是1次
- valid:設置檢查頻率,默認60s
- off:禁用緩存
語法格式: 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 max=1000 inactive=20s valid=1m min_uses=2;
nginx日志調試技巧
設置 Nginx 僅記錄來自於你的 IP 的錯誤
當你設置日志級別成 debug,如果你在調試一個在線的高流量網站的話,你的錯誤日志可能會記錄每個請求的很多消息,這樣會變得毫無意義。
在events{...}
中配置如下內容,可以使 Nginx 記錄僅僅來自於你的 IP 的錯誤日志。
events {
debug_connection 1.2.3.4;
}
調試 nginx rewrite 規則
調試rewrite規則時,如果規則寫錯只會看見一個404頁面,可以在配置文件中開啟nginx rewrite日志,進行調試。
server {
error_log /var/logs/nginx/example.com.error.log;
rewrite_log on;
}
rewrite_log on;
開啟后,它將發送所有的 rewrite 相關的日志信息到 error_log 文件中,使用 [notice] 級別。隨后就可以在error_log 查看rewrite信息了。
使用location記錄指定URL的日志
server {
error_log /var/logs/nginx/example.com.error.log;
location /static/ {
error_log /var/logs/nginx/static-error.log debug;
}
}
配置以上配置后,/static/ 相關的日志會被單獨記錄在static-error.log文件中。
nginx日志共三個參數
access_log: 定義日志的路徑及格式。
log_format: 定義日志的模板。
open_log_file_cache: 定義日志文件緩存。
proxy_set_header X-Forwarded-For :如果后端Web服務器上的程序需要獲取用戶IP,從該Header頭獲取。proxy_set_header X-Forwarded-For $remote_addr;
常用例子
main格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$upstream_addr $upstream_response_time $request_time ';
access_log logs/access.log main;
json格式
log_format logstash_json '{"@timestamp":"$time_iso8601",'
'"host": "$server_addr",'
'"client": "$remote_addr",'
'"size": $body_bytes_sent,'
'"responsetime": $request_time,'
'"domain": "$host",'
'"url":"$request_uri",'
'"referer": "$http_referer",'
'"agent": "$http_user_agent",'
'"status":"$status",'
'"x_forwarded_for":"$http_x_forwarded_for"}';
解釋:
$uri
請求中的當前URI(不帶請求參數,參數位於$args
),不同於瀏覽器傳遞的$request_uri
的值,它可以通過內部重定向,或者使用index指令進行修改。不包括協議和主機名,例如/foo/bar.html。
$request_uri
這個變量等於包含一些客戶端請求參數的原始URI,它無法修改,請查看$uri
更改或重寫URI。
也就是說:$request_uri
是原始請求URL,$uri
則是經過nginx處理請求后剔除參數的URL,所以會將漢字表現為union。
坑點:
使用$uri
可以在nginx對URL進行更改或重寫,但是用於日志輸出可以使用$request_uri
代替,如無特殊業務需求,完全可以替換。
壓縮格式
日志中增加了壓縮的信息。
http {
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
server {
gzip on;
access_log /spool/logs/nginx-access.log compression;
...
}
}
upstream格式
增加upstream消耗的時間。
http {
log_format upstream_time '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"'
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
server {
access_log /spool/logs/nginx-access.log upstream_time;
...
}
}
參考文檔
統計status 出現的次數
awk '{print $9}' access.log | sort | uniq -c | sort -rn
36461 200
483 500
87 404
9 400
3 302
1 499
1 403
1 301
顯示返回302狀態碼的URL。
awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn
1 /wp-login.php
1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131
1 /wp-admin/
參考文檔
How to Configure Custom Access and Error Log Formats in Nginx
如何在Nginx中配置自定義訪問和錯誤日志格式