因为业务需要,在访问页面中会添加一个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的值。