在nginx.conf文件或vhosts/*.conf文件中的access_log日志中指定級別為main。
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - [$time_iso8601] - $msec - $status - $request_time - $body_bytes_sent - "$http_host" - "$request" - "$http_referer" - "$http_user_agent" - "$http_x_forwarded_for"';
#設置日志默認存儲目錄 access_log logs/access.log main;
error_log logs/error.log;
說明:
$remote_addr, $http_x_forwarded_for(反向) 記錄客戶端IP地址 $remote_user 記錄客戶端用戶名稱 $request 記錄請求的URL和HTTP協議 $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 通用日志格式下的本地時間。
如要根據不同站點設置不同日志存儲目錄,則在以下位置配置:
server { listen 80; server_name m.test.cc; #不同站點設置不同的日志存儲路徑,可以使用變量 access_log logs/$server_name.log main; root "F:/test/public_html"; location / { try_files $uri $uri/ /index.php?$query_string; index index.html index.htm index.php; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }