在k8s环境Nginx Ingress记录Cookie和Header信息

在kubenetes中需要记录header、cookies等信息,便于日志追踪、链路追踪等分析。

1.获取Cookies字段添加到日志格式

修改Ingres ConfigMap文件 json日志格式添加自定义的cookie字段”user_id”: “$affefdgx_id”

data:
  log-format-upstream: '{"msec":"$msec","connection":"$connection","connection_requests":"$connection_requests","pid":"$pid","request_id":"$request_id",
 "request_length":"$request_length","remote_addr":"$remote_addr","remote_user":"$remote_user","remote_port":"$remote_port","time_local":"$time_local","time_iso8601":"$time_iso8601","request":"$request","request_uri":"$request_uri","args":"$args","status":"$status","body_bytes_sent":"$body_bytes_sent","bytes_sent":"$bytes_sent","http_referer":"$http_referer","http_user_agent":"$http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for","http_host":"$http_host","server_name":"$server_name","request_time":"$request_time","upstream":"$upstream_addr","upstream_connect_time":"$upstream_connect_time","upstream_header_time":"$upstream_header_time","upstream_response_time":"$upstream_response_time","upstream_response_length":"$upstream_response_length","upstream_cache_status":"$upstream_cache_status","ssl_protocol":"$ssl_protocol","ssl_cipher":"$ssl_cipher","scheme":"$scheme","request_method":"$request_method","server_protocol":"$server_protocol","pipe":"$pipe","gzip_ratio":"$gzip_ratio","http_cf_ray":"$http_cf_ray","proxy_alternative_upstream_name":"$proxy_alternative_upstream_name","req_id":"$req_id","cookie_token":"$cookie_token"}'

2.获取Headers字段添加到日志格式

2.1 参数详解

  • http_NAME #匹配任意请求头字段;

  • 变量名中的后半部分NAME可以替换成任意请求头字段;

  • 如在配置文件中需要获取http请求头:”Accept-Language”,http_accept_language即可 可在自定义header字段前加http_,即可将指定的自定义header字段打印到log中。

  • 以下为示例:

    • 添加Header:Header-Token到日志
      • "header_token":"$http_header_token"
    • 添加Header:header_token到日志
      • "header_token":"$http_header_token"
data:
  log-format-upstream: '{"msec":"$msec","connection":"$connection","connection_requests":"$connection_requests","pid":"$pid","request_id":"$request_id",
 "request_length":"$request_length","remote_addr":"$remote_addr","remote_user":"$remote_user","remote_port":"$remote_port","time_local":"$time_local","time_iso8601":"$time_iso8601","request":"$request","request_uri":"$request_uri","args":"$args","status":"$status","body_bytes_sent":"$body_bytes_sent","bytes_sent":"$bytes_sent","http_referer":"$http_referer","http_user_agent":"$http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for","http_host":"$http_host","server_name":"$server_name","request_time":"$request_time","upstream":"$upstream_addr","upstream_connect_time":"$upstream_connect_time","upstream_header_time":"$upstream_header_time","upstream_response_time":"$upstream_response_time","upstream_response_length":"$upstream_response_length","upstream_cache_status":"$upstream_cache_status","ssl_protocol":"$ssl_protocol","ssl_cipher":"$ssl_cipher","scheme":"$scheme","request_method":"$request_method","server_protocol":"$server_protocol","pipe":"$pipe","gzip_ratio":"$gzip_ratio","http_cf_ray":"$http_cf_ray","proxy_alternative_upstream_name":"$proxy_alternative_upstream_name","req_id":"$req_id","cookie_token":"$cookie_token","header_token":"$http_header_token"}'