Setting a trace id in nginx load balancer

前端 未结 4 673
臣服心动
臣服心动 2021-01-30 11:13

I\'m using nginx as a load balancer in front of several upstream app servers and I want to set a trace id to use to correlate requests with the app server logs. What\'s the bes

4条回答
  •  迷失自我
    2021-01-30 11:28

    In most cases you don't need a custom module, you can simply set a header with a combination of embedded variables of http_core_module which is (most probably) unique. Example:

      location / {
          proxy_pass http://upstream;
          proxy_set_header X-Request-Id $pid-$msec-$remote_addr-$request_length;
      }
    

    This would yield a request id like "31725-1406109429.299-127.0.0.1-1227" and should be "unique enough" to serve as a trace id.

提交回复
热议问题