nginx日志中记录cookie信息的配置参考

发布时间:2020-01-02编辑:脚本学堂
本文介绍下,在nginx日志中记录cookie信息的具体配置方法,有需要的朋友参考下吧。

本节介绍,通过nginx日志记录cookie信息,以供数据分析人员进行有用信息获取的方法。

例1,
 

复制代码 代码示例:

server {
  listen80;
  server_name  www.jb200.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.log  main;

  location / {
    root   /Data/webapps/www.aslibra.com/;
    index  index.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`

例2,
记录手机网站的cookie信息到access.log。
配置:
 

复制代码 代码示例:
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;
}

例3,
 

复制代码 代码示例:

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" ';