1. 程式人生 > >Nginx 訪問日誌配置

Nginx 訪問日誌配置

cte 4.0 clr live get ide onf 定義 window

[[email protected] conf]# cat nginx.conf
worker_processes  1;
error_log  logs/error.log  error;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include     vhosts/*.conf;
    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘   # 先定義日誌格式,main是日誌格式的名字
‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log logs/access.log main; # 使用日誌格式,也可以把這一行放到想記錄訪問日誌的虛擬主機配置文件中去
}
$remote_addr :記錄訪問網站的客戶端地址
$remote_user :記錄遠程客戶端用戶名稱
$time_local :記錄訪問時間與時區
$request :記錄用戶的 http 請求起始行信息
$status :記錄 http 狀態碼,即請求返回的狀態,例如 
200404502 等 $body_bytes_sent :記錄服務器發送給客戶端的響應 body 字節數 $http_referer :記錄此次請求是從哪個鏈接訪問過來的,可以根據 referer 進行防盜鏈設置 $http_user_agent :記錄客戶端訪問信息,如瀏覽器、手機客戶端等 $http_x_forwarded_for :當前端有代理服務器時,設置 Web 節點記錄客戶端地址的配置,此參數生效的前提是代理服務器上也進行了相關的 x_forwarded_for 設置

產生的日誌:

192.168.5.1 - - [25/May/2017:18:27:51 +0800
] "GET / HTTP/1.1" 200 12 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)" "-"
$remote_addr 對應的是 192.168.5.1 ,即客戶端的 IP 地址
$remote_user 對應的是 - ,沒有遠程用戶,所以用 - 填充
$time_local 對應的是 [25/May/2017:18:27:51 +0800]
$request 對應的是 "GET / HTTP/1.1"
$status 對應的是狀態碼 200 ,表示訪問正常
$body_bytes_sent 對應的是 12 字節,即響應 body 的大小
$http_referer 對應的是 "-" ,由於是直接打開域名瀏覽的,因此 referer 沒有值
$http_user_agent 對應的是 "Mozilla/4.0 (compatible; MSIE........)"
$http_x_forwarded_for 對應的是 "-" ,因為 Web 服務沒有使用代理,所以用 "-" 填充

Nginx 訪問日誌配置