PS:Nginx日誌相關指令主要有兩條,一條是log_format,用來設置日誌格式,另一條是access_log,用來指定日誌文件的存放路徑、類型、緩存大小等,通常放在Nginx的默認主配置文件/etc/nginx/nginx.conf 。php
說明: 該nginx是1.8 在 商城教程中html
日誌使用教程:nginx
(一) 在 nginx的Server段中,添加 access_log 字段,用來定義 每一個 Server 的日誌生成路徑 及 指定格式算法
例如: 瀏覽器
(二) 在 http中定義 log_format,例如: 緩存
(三)從新加載nginx : ./nginx -s reload服務器
(四)nginx完整配置以下session
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; access_log logs/test.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
(五)自定義nginx日誌格式 app
Nginx的log_format有不少可選的參數用於標示服務器的活動狀態,默認的是:
‘$remote_addr – $remote_user [$time_local] 「$request」 ‘
‘$status $body_bytes_sent 「$http_referer」 ‘
‘」$http_user_agent」 「$http_x_forwarded_for」‘;tcp
若是要記錄更詳細的信息須要本身修改log_format,具體可設置的參數格式及說明以下:
參數 | 說明 | 示例 |
$remote_addr | 客戶端地址 | 219.227.111.255 |
$remote_user | 客戶端用戶名稱 | — |
$time_local | 訪問時間和時區 | 18/Jul/2014:17:00:01 +0800 |
$request | 請求的URI和HTTP協議 | 「GET /article-10000.html HTTP/1.1」 |
$http_host | 請求地址,即瀏覽器中你輸入的地址(IP或域名) | www.ha97.com 198.98.120.87 |
$status | HTTP請求狀態 | 200 |
$upstream_status | upstream狀態 | 200 |
$body_bytes_sent | 發送給客戶端文件內容大小 | 1547 |
$http_referer | url跳轉來源 | https://www.google.com/ |
$http_user_agent | 用戶終端瀏覽器等信息 | 「Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; |
$ssl_protocol | SSL協議版本 | TLSv1 |
$ssl_cipher | 交換數據中的算法 | RC4-SHA |
$upstream_addr | 後臺upstream的地址,即真正提供服務的主機地址 | 10.36.10.80:80 |
$request_time | 整個請求的總時間 | 0.165 |
$upstream_response_time | 請求過程當中,upstream響應時間 | 0.002 |