1. 程式人生 > >Nginx 日誌中記錄cookie

Nginx 日誌中記錄cookie

nginx

因開發要求,在Nginx日誌中需要記錄Cookie信息,以便開發查詢系統發生了什麽,我的日誌是以json格式顯示,需要在nginx.conf文件中添加如下信息:

1,#vim nginx.conf

http {
include mime.types;
default_type application/octet-stream;
log_format logstash_json ‘{"@timestamp":"$time_iso8601",‘
‘"host":"$server_addr",‘
‘"clientip":"$remote_addr",‘

‘"size":$body_bytes_sent,‘
‘"responsetime":$request_time,‘
‘"upstreamtime":"$upstream_response_time",‘
‘"upstreamhost":"$upstream_addr",‘
‘"http_host":"$host",‘
‘"url":"$uri",‘
‘"xff":"$http_x_forwarded_for",‘
‘"referer":"$http_referer",‘
‘"agent":"$http_user_agent",‘
‘"@req_body":"$request_body",‘
‘"$http_cookie":"$http_cookie",‘ 添加這行
‘"status":"$status"}‘;

location ~* ^/test/.*$ {
proxy_hide_header Set-Cookie; 添加這行
proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.9.54.1:797;
2,再查看Nginx日誌,就可以顯示Cookie信息了

技術分享

Nginx 日誌中記錄cookie