Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

nginx access 日誌分析與自訂配置

nginx 的日誌分為 access log 與 error log:前者記錄哪些用戶、哪些頁面、瀏覽器、IP 等存取資訊,後者記錄伺服器錯誤。

以如下一條 access 記錄為例:

201.158.69.116 - - [03/Jan/2013:21:17:20 -0600] fwf[-] tip[-] 127.0.0.1:9000 0.007 0.007 MX pythontab.com GET /html/test.html HTTP/1.1 "200" 2426 "https://a.com" "es-ES,es;q=0.8" "Mozilla/5.0 ... Chrome/23.0.1271.97 Safari/537.11"

從中可拆解出多項資訊:

1. 客戶端 IP,如 201.158.69.116;
2. 存取時間,如 [03/Jan/2013:21:17:20 -0600];
3. 存取埠,如 127.0.0.1:9000;
4. 響應時間與 5. 請求時間,皆為 0.007;
6. 地理位置代碼,如 MX(墨西哥);
7. 請求的 host,如 pythontab.com;
8. 請求方式,如 GET;
9. 去除 host 後的 URL,如 /html/test.html;
10. 狀態碼,200 代表成功、404 不存在、301 永久重定向等;
11. 頁面大小(位元組),如 2426;
12. 來源頁 referer,如 https://a.com;
13. 瀏覽器語言,如 es-ES,es;q=0.8;
14. 瀏覽器其他資訊(版本、類型等)。

其實 access 日誌格式並非一成不變,而是可自訂。在 nginx.conf 中找到 log_format 即可修改,例如:

log_format main '$remote_addr - $remote_user [$time_local] '
'fwf[$http_x_forwarded_for] tip[$http_true_client_ip] '
'$upstream_addr $upstream_response_time $request_time '
'$geoip_country_code '
'$http_host $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_accept_language" "$http_user_agent"';

再用 access_log 指定存放位置與格式,如 access_log /home/serversoft/nginx/log/access.log main;。access.log 也可按日期切分,方便檢視與處理。

Copyright © Linux教程網 All Rights Reserved