What is the use of recording cookies?
Sometimes we need to count the Uvs (independent visitors) through the Web server's access logs and analyze the user's behavior accordingly. UV is a statistic based on cookie data. The advantages of UV relative to IP are: IP is a reflection of the network virtual address object concept, UV is a reflection of the actual user concept, more accurately correspond to an actual browser. Using UV as a statistic, you can more accurately understand how many visitors actually come to the corresponding page in the unit time.
How do I record cookies? Nginx:
In Nginx's configuration file, cookies can be accessed via $http_cookie.
To record a cookie, you need to modify the nginx.conf configuration file. Here's a look at the specific configuration method.
Find the following code,
1 #Log_format main ' $remote _addr-$remote _user [$time _local] "$request" '2 # ' $status $body _bytes_sent ' $http _referer ' 3 # ' "$http _user_agent" "$http _x_forwarded_for" ';
If you want to record the entire cookie, add a new Log_format below
1 log_format access_with_cookie ' $remote _addr-$remote _user [$time _local] "$request" '2 ' $status $body _bytes_sent "$http _referer" '3 ' "$http _user_agent" "$http _x_forwarded_for "$http _cookie";
and add it in the right place.
Access_log /usr/share/nginx/logs/access_with_cookie.log Access_with_cookie;
If you want to record a portion of a cookie, you need to intercept the contents of the $http_cookie.
Now suppose we set a cookie in PHP,
<? PHP Setcookie Time () + 3600*24); ? >
$http _cookie will probably be like this.
Uuid=137c6bae-de1f-4f5f-51ed-5e1aa1b55a17
If you want to just record 137c6bae-de1f-4f5f-51ed-5e1aa1b55a17.
You need to add the nginx.conf server segment
1 if ($http _cookie ~* ". *uuid= (. *) (?:; |$)") 2 {3 set $cookie _uuid $; 4 }
and modify Logformat, use $cookie_uuid instead of $http_cookie
The above configuration involves nginx regular expressions. I'm not very familiar with regular expressions, but here is my understanding and not the right thing to say.
Represents the contents of the first parenthesis inside a regular expression.
Well, the cookie in the Nginx log is probably the way it is.
Apache
Nginx configuration on the network more articles, but Apache relatively few. Explain my method, the principle is the same, but there are some differences in grammar.
Apache has preset a way to get a cookie field:%{varname}c, so it's relatively simple to configure.
Edit the httpd.conf to find the following content.
Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined
Add 1 lines above it,
Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ " \"%{uuid}c\ "" Combined_with_cookie
%{varname}c represents the value of VARNAME in the cookie. Only version 0 cookies are supported. The Logformat for Apache can be found here, with detailed instructions.
Modify
Customlog "Logs/access_log" common
For
Customlog "Logs/access_log" Combined_with_cookie
In addition, the value of the entire Cookie can be obtained using%{cookie}i.
The above is only a summary of their own experience, does not guarantee its correctness, for reference only.
Nginx and Apache configuration log format record cookie