1. 在要添加的server中添加获取cookie的信息

    server {
                    listen          80;
                    server_name     jxjymember.cdeledu.com;
                    set $JXJYMEMBERCookie "";

                    #此处如果想取全部cookie 可以写成  

                    #if ( $http_cookie ~* "(.*)$" ){
                    if ( $http_cookie ~* "JXJYMEMBERCookie=([A-Za-z0-9_]*)" ){
                       set $JXJYMEMBERCookie $1;
                    }


                    access_log      logs/jxjymember.cdeledu.com_access.log main;
                    error_log      logs/error_jxjymember.cdeledu.com.log;
                    location / {
                              #
                              proxy_pass              http://cdel_jxjy_member;
                              proxy_redirect          off;
                              proxy_set_header        Host $host;
                              proxy_set_header        X-Real-IP $remote_addr;
                              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                              client_max_body_size    10m;
                              client_body_buffer_size 512k;
                              proxy_connect_timeout   300;
                              proxy_send_timeout      300;
                              proxy_read_timeout      300;
                              proxy_buffer_size       64k;
                              proxy_buffers           32 64k;
                              proxy_busy_buffers_size 128k;
                              proxy_temp_file_write_size 64k;
                    }
            }

    主要添加的地方都标红了,说明下:第一处的意思是:定义一个JXJYMEMBERCookie变量,变量的内容从http的cookie去取,取的规则是匹配后面的正则JXJYMEMBERCookie=([A-Za-z0-9_]*),其实http_cookie这个里面的值是一个一个cookie的值,中间以“;”分隔。

  2. log_format main中添加上要添加的变量名。

    log_format main      '$proxy_add_x_forwarded_for - $remote_user [$time_local] '
                             '"$request" $status $bytes_sent '
                             '"$http_referer" "$http_user_agent" $JXJYMEMBERCookie ';

  3. 效果

    取一个的

    wKioL1Pi1UbAsdpBAAGnt1et0S8465.jpg

    取全部的

    wKiom1Pi4reQd447AAIADzYDerY000.jpg