需求描述:该需求主要是为了查找日志方便,一个http请求从nginx服务器到php业务日志的串联。
环境需求:nginx 要1.11以上版本支持生成request_id。
部署方案如下:
1. 接入机nginx.conf 在server段添加 如下配置:
proxy_set_header X-Request-Id $request_id;在log_format 添加配置:'”$request_id”‘
例子如下,见蓝色字体:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '"$request_id"';
server {
listen 80;
server_name localhost;
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
proxy_pass 192.168.174.143:80;
proxy_set_header X-Request-Id $request_id;
}
2.此时在后端服务器就可以获取从接入机传过来的X-Request-Id,此时只需要在后端mapi对应的nginx服务器中的log添加进来就可以了.配置如下(见蓝色字体):
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"''"$http_x_request_id"';
3.至此就可以用我们熟悉的php全局数组$_SERVER来获取requestId了。