Nginx日志记录Cookie 学习笔记


事例一:
server {
listen 80;
server_namewww.aslibra.com;
set $aslibra_auth "";
if ( $http_cookie ~* "aslibra_auth=(.+)(?:;|$)" ){
set $aslibra_auth $1;
}

log_format main'$proxy_add_x_forwarded_for - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" $aslibra_auth ';
access_log/Data/log/nginx-access.logmain;

location / {
root /Data/webapps/www.aslibra.com/;
indexindex.html index.htm;
}

error_page 500 502 503 504/50x.html;
location = /50x.html {
root html;
}

}


参考分割日志的脚本:

#!/bin/bash
log_dir="/Data/log"
time=`date +%Y%m%d`
/bin/mv${log_dir}/nginx-access.log ${log_dir}/nginx-access.$time.log
kill -USR1 `cat/var/run/nginx.pid`


事例(二)

现有一需求,需要把我们手机网站的cookie信息记录到access.log里,数据挖掘部门需要根据这个来统计用户行为。其实我还真没有这样记录过日志,后来百度了一下,发现Nginx确实很强大。
具体实现看配置:
server
{
listen 80;
server_name 192.168.1.101;
#setting cookie log
#if ( $http_cookie ~* "wap_auth=(. )(?:;|$)" )
if ( $http_cookie ~* "(.*)$")
{
set $wap_cookie $1;
}
index index.php index.htm index.html;
add_header Load-Balancing $server_addr;
root /server/www/apps/wap_v2;
rewrite ^/css/(.*)$ /media/css/$1 last;

location /logs {
alias /data/nginx/logs/;
}
location ~* .*\.(php|html)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /nginx-status {
stub_status on;
allow 192.168.1.171;
deny all;
}
log_format wap_access '$remote_addr $host \
$server_addr [$time_local] "$request \
" ' '$status $body_bytes_sent \
"$http_referer" ' '"$http_user_agent\
" "$wap_cookie"';
access_log /data/nginx/logs/access_wap.log wap_access;
}


事例(三)

set $user_id "0";
#获取uuid
set $uuid "_";
if ( $http_cookie ~* "user_id=([0-9]*)" ){
set $user_id $1;
}


if ( $http_cookie ~* "_uuid=([A-Za-z0-9 ]*)" ){
set $uuid $1;
}



log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $user_id "$uuid" ';



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值