kubernetes 自定义ingres-nginx日志添加cookie Header字段

作者: root007 分类: kubernetes,Linux 发布时间: 2019-11-06 10:12

配置ingress uuid字段

需要把cookie字段添加到日志中方便业务分析,方法和nginx配置差不多

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

apiVersion: v1
data:
  log-format-upstream: '{"time": "$time_iso8601","remote_addr": "$remote_addr","x-forward-for":
    "$proxy_add_x_forwarded_for","request_id": "$req_id","remote_user": "$remote_user","bytes_sent":
    "$bytes_sent","request_time": "$request_time","status": "$status","vhost": "$host","request_proto":
    "$server_protocol","path": "$uri","request_query": "$args","request_length": "$request_length","duration":
    "$request_time","method": "$request_method","http_referrer": "$http_referer","http_user_agent":
    "$http_user_agent","user_id": "$affefdgx_id"}'
kind: ConfigMap
metadata:
  annotations:
  labels:
    app: ingress-nginx
  name: nginx-configuration
  namespace: ingress-nginx

修改ingress对象 通过annotations的nginx.ingress.kubernetes.io/server-snippet:

nginx.ingress.kubernetes.io/server-snippet: |
        set $affefdgx_id "-";
        if ( $http_cookie ~* "affefdgx=(\S+)(;.*|$)"){
            set $affefdgx_id $1;
        }

获取ingress-nginx记录用户请求Header
把自定义头部加入日志

参数详解 

http://nginx.org/en/docs/http/ngx_http_core_module.html#var_cookie_
$http_NAME #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",$http_accept_language即可
可在自定义header字段前加http_,即可将指定的自定义header字段打印到log中
apiVersion: v1
data:
  log-format-upstream: '{"time": "$time_iso8601","remote_addr": "$remote_addr","x-forward-for":
    "$proxy_add_x_forwarded_for","request_id": "$req_id","remote_user": "$remote_user","bytes_sent":
    "$bytes_sent","request_time": "$request_time","status": "$status","vhost": "$host","request_proto":
    "$server_protocol","path": "$uri","request_query": "$args","request_length": "$request_length","duration":
    "$request_time","method": "$request_method","http_referrer": "$http_referer","http_user_agent":
    "$http_user_agent","wx_uuid":"$http_useruuid"}'
kind: ConfigMap

发表评论

电子邮件地址不会被公开。 必填项已用*标注