Nginx訪問日誌(access_log)配置
阿新 • • 發佈:2018-04-06
location 緩沖區 per 防盜鏈 rem mime pes erro error
Nginx訪問日誌主要有兩個參數控制
log_format #用來定義記錄日誌的格式(可以定義多種日誌格式,取不同名字即可)
access_log #用來指定日至文件的路徑及使用的何種日誌格式記錄日誌
lof_format的默認值:
# 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的默認值:
#access_log logs/access.log main;
log_format語法格式及參數語法說明如下:
log_format <NAME> <Strin---g>; 關鍵字 格式標簽 日誌格式 關鍵字:其中關鍵字error_log不能改變 格式標簽:格式標簽是給一套日誌格式設置一個獨特的名字 日誌格式:給日誌設置格式 log_format格式變量: $remote_addr #記錄訪問網站的客戶端地址 $remote_user #遠程客戶端用戶名 $time_local #記錄訪問時間與時區 $request #用戶的http請求起始行信息 $status #http狀態碼,記錄請求返回的狀態碼,例如:200、301、404等 $body_bytes_sent #服務器發送給客戶端的響應body字節數 $http_referer #記錄此次請求是從哪個連接訪問過來的,可以根據該參數進行防盜鏈設置。 $http_user_agent #記錄客戶端訪問信息,例如:瀏覽器、手機客戶端等 $http_x_forwarded_for #當前端有代理服務器時,設置web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器也要進行相關的x_forwarded_for設置
access_log語法格式及參數語法說明如下:
access_log <FILE> <NAME>; 關鍵字 日誌文件 格式標簽 關鍵字:其中關鍵字error_log不能改變 日誌文件:可以指定任意存放日誌的目錄 格式標簽:給日誌文件套用指定的日誌格式 其他語法: access_log off; #關閉access_log,即不記錄訪問日誌 access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; 說明: buffer=size #為存放訪問日誌的緩沖區大小 flush=time #為緩沖區的日誌刷到磁盤的時間 gzip[=level] #表示壓縮級別 [if = condition] #表示其他條件
一般場景這些參數都無需配置,極端優化才有可能會考慮這些參數。
lof_format參數的標簽段位置:
http
access_log參數的標簽段位置:
http, server, location, if in location, limit_except
參考資料:http://nginx.org/en/docs/http/ngx_http_log_module.html
Nginx配置訪問日誌過程:
(1)創建log_format語句
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include status.conf;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
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;
server {
listen 80;
server_name localhost;
rewrite ^/.* http://www.wl.com permanent;
}
include vhost/*.conf;
}
(2)插入access_log語句
server {
access_log /data/log/www;
listen 80;
server_name abc.com www.wl.com;
location / {
root /data/www/www;
index index.html index.htm;
}
error_log logs/error_www.wl.com.log error;
access_log logs/access_www.wl.com.log main;
#新增內容↑
}
(3)重啟服務
nginx -t
nginx -s reload
Nginx訪問日誌(access_log)配置