Add cookie information about nginx logs
1. Obtain all cookie information
This is more convenient, add $http_cookie directly to the nginx.conf file
log_format main '[$time_local] - $remote_addr:$remote_port - $remote_user - $upstream_addr $upstream_status $upstream_response_time - ' '"$request" $status $bytes_sent $request_time ' '"$http_referer" - "$http_user_agent" ' '"$http_cookie"';#Add a $http_cookie
Step 2: Add main after access.log in server
access_log logs/item_access.log main; error_log logs/item_error.log ;
Second, get the value of a single cookie
Sometimes if you think a cookie is important, you need to take it out separately to avoid a lot of trouble.
First, also define a value in the nginx.conf file, such as $my_cookie
log_format main '[$time_local] - $remote_addr:$remote_port - $remote_user - $upstream_addr $upstream_status $upstream_response_time - ' '"$request" $status $bytes_sent $request_time ' '"$http_referer" - "$http_user_agent" ' '"$my_cookie" "$http_cookie"';#This defines $my_cookie and $http_cookie, which can actually get # $my_cookie can only get value
Second, we still need to change the contents of the server.
server { listen 80; server_name XXXX.****. com ; access_log logs/item_access.log main; # main Required error_log logs/item_error.log ; set $my_cookie ""; if ($http_cookie ~* "my_cookie=(.+?) (?=;|$) ") { set $my_cookie $1; }
Third, get the value of multiple cookies
What if more than one cookie value is important, say two cookies need to be retrieved? Repeat step 2
Add the variable $my_cookie2 to the nginx.conf log format
Add a set $my_cookie2 to the server section
log_format main '[$time_local] - $remote_addr:$remote_port - $remote_user - $upstream_addr $upstream_status $upstream_response_time - ' '"$request" $status $bytes_sent $request_time ' '"$http_referer" - "$http_user_agent" ' '"$my_cookie" - "$my_cookie2" - "$http_cookie"'; server { listen 80; server_name XXXX.***. com ; access_log logs/item_access.log main; # main Required error_log logs/item_error.log ; set $my_cookie ""; if ($http_cookie ~* "my_cookie=(.+?) (?=;|$) ") { set $my_cookie $1; } set $my_cookie2 ""; if ($http_cookie ~* "my_cookie2=(.+?) (?=;|$) ") { set $my_cookie2 $1; }
I have achieved it here, but the name is different. Let's try it ourselves.
PS: Add cookie information to nginx logs
1. Add information about getting cookies to the server you want to add
server { listen 80; server_name jxjymember.cdeledu.com; set $JXJYMEMBERCookie ""; #If you want to get all cookies here, you can write #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; } }
The main places added are marked in red. The following explanation: the first place means: define a JXJYMEMBERCookie variable, the content of the variable is taken from http cookie, and the rule taken is to match the regular JXJYMEMBERCookie=([A-Za-z0 -9_]*). In fact, the value in http_cookie is a cookie value, separated by ";".
2. In log_main format, add the names of variables you don't want to add.
log_format main '$proxy_add_x_forwarded_for - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" $JXJYMEMBERCookie ';
3. effect
Take one.
Take all of them.
summary
The above is the nginx log introduced by Xiaobian to you to add cookie information. I hope it will help you. If you have any questions, please leave a message to me. Xiaobian will reply to you in time. Thank you very much for your support!