有没有办法logging每个请求唯一的ID为Nginx?

似乎有几个第三方模块为此

  • https://github.com/newobj/nginx-x-rid-header
  • https://github.com/hhru/nginx_requestid

但是,如果可能的话,我不想重buildnginx,我相信这是要求使用这两个模块。 我最初的尝试是使用$msec$pid但是没有成功(我有一个进程在同一毫秒服务两个请求)。 我读过关于$connection ,似乎这将与$msec一起工作。 这会工作,还是有没有更好的方式,而不使用第三方模块?

nginx版本v1.11更新(2016年5月):

http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_id

你可以使用$request_id

由16个随机字节生成的唯一请求标识符,以hex(1.11.0)

你可以使用nginx-extras并使用embedded式Perl或Lua。

$ sudo apt-get install libossp-uuid-perl

在你的nginxconfiguration中:

 perl_require "Data/UUID.pm"; perl_set $request_uuid 'sub { my $ug = new Data::UUID; return $ug->create_str(); }'; 

然后在给定的location

 proxy_set_header Request-Id $request_uuid 

我们正在生产中使用它,并且对我们在后端服务中提供的更好的跟踪感到非常高兴。

如果你可以在nginx中使用nginx-extrasperl

然后你可以使用perl_set设置请求variables

 perl_set $uuid 'sub { return join "", map{(a..z,A..Z,0..9)[rand 62]} 0..7; }'; 

如果您需要更多详细信息:http: //yozik04.blogspot.com/2014/12/nginx-request-id-using-perl.html