Regular Expression - How does Nginx parse UserId from Cookie and put it into Access Log?
阿神
阿神 2017-05-16 17:29:39
0
2
687

It’s very frustrating. For requests to visit the homepage, the uid can be analyzed from the cookie, but other requests cannot.
Print the cookie directly, there is no difference between the homepage and other requests

My Nginx configuration file

        set $uid "-";
        if ( $http_cookie ~* "uid=(\S+)(;.*|$)"){
            set $uid ; 
        }

        log_format s_access    '[$time_local] $remote_addr - "$request" - '
                                '$status - $body_bytes_sent - "$http_referer" - '
                                '"$http_user_agent" - $request_time - "$uid" "$http_cookie"';

Two Access Logs, the upper one is access/list cookie. In the last "", the previous "" is empty. It seems that if comes in, but the uid is empty; the next one is access/cookie. Like the previous one, within "", the previous "" has a value and is recognized.
Very depressing!

[04/Dec/2012:21:44:43 +0800] 145.122.142.124 - "GET /list HTTP/1.1" - 200 - 18573 - "-" - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.60 Safari/537.11" - 0.079 - "" "__c_uactiveat=1350108823468; __c_review=46; __c_last=1351061042906; __c_visitor=1350104003730701; uid=103; __utma=224849432.425128387.1339588739.1354606821.1354625898.809; __utmb=224849432.12.10.1354625898; __utmc=224849432; __utmz=224849432.1354108162.783.24.utmcsr=e.weibo.com|utmccn=(referral)|utmcmd=referral|utmcct=/2036070420/z7ixpvGv4; sess=50b8f.796"

[04/Dec/2012:21:45:18 +0800] 145.122.142.124 - "GET / HTTP/1.1" - 200 - 13449 - "-" - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.60 Safari/537.11" - 0.248 - "1030000000091320" "__c_uactiveat=1350108823468; __c_review=46; __c_last=1351061042906; __c_visitor=1350104003730701; uid=103; __utma=224849432.425128387.1339588739.1354606821.1354625898.809; __utmb=224849432.13.10.1354625898; __utmc=224849432; __utmz=224849432.1354108162.783.24.utmcsr=e.weibo.com|utmccn=(referral)|utmcmd=referral|utmcct=/2036070420/z7ixpvGv4; sess=50b8f.796"
阿神
阿神

闭关修行中......

reply all(2)
仅有的幸福

$cookie_xxx xxx is the cookie key

黄舟

Cookie needs to be accessed once and then returned to the client.
There is no cookie when visiting the client for the first time

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template