因爲業務需要,在訪問頁面中會添加一個cookie,方便業務的分析。
網上搜索“nginx 訪問日誌中添加cookie”出現很多訪問,不過大家把內容全放在一起,甚至有一些訪問中在server模塊中出現log_format,因此把一些理解整理了一下。
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" $query_cookie';
server {
set $query_cookie "-";#設置默認值
if ( $http_cookie ~* "uuid=(\S+)(;.*|$)"){
set $query_cookie $1;
}
access_log logs/api-access.log main;
listen 80;
server_name www.peter.com;
root D:/git/workspace/www.peter.com/;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
uuid即爲你要關注的cookie名,如果用戶在瀏覽網站時,如果cookie中包含uuid,則會在access日誌中包含uuid的值。