问题描述
我正在使用nginx作为多个上游应用程序服务器前的负载平衡器,并且我想设置一个跟踪ID,以用于将请求与应用程序服务器日志相关联.在Nginx中做到这一点的最佳方法是什么,为此有一个好的第三方模块吗?
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 best way to do that in Nginx, is there a good 3rd party module for this?
否则,一种非常简单的方法是将其基于时间戳(如果不够精确,则可能加上一个随机数)并将其设置为请求的额外标头,但是我在文档中看到的唯一set_header命令是用于设置响应头.
Otherwise a pretty simple way would be to base it off of timestamp (possibly plus a random number if that's not precise enough) and set it as an extra header on the request, but the only set_header command I see in the docs is for setting a response header.
推荐答案
在大多数情况下,您不需要自定义模块,只需设置一个标头,其中包含http_core_module的嵌入式变量的组合(很可能)是唯一的.示例:
In most cases you don't need a custom module, you can simply set aheader with a combination of embedded variables of http_core_modulewhich is (most probably) unique. Example:
location / {
proxy_pass http://upstream;
proxy_set_header X-Request-Id $pid-$msec-$remote_addr-$request_length;
}
这将产生一个请求ID,例如"31725-1406109429.299-127.0.0.1-1227"并且应该足够独特"以用作跟踪ID.
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.
这篇关于在Nginx负载均衡器中设置跟踪ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!