如何将数据从php-fpm传递到nginx访问日志中


How to pass data from php-fpm into nginx access log?

我正试图通过在nginx中记录请求URL和$request_time来查找PHP应用程序中的瓶颈。不幸的是,$request_time包含网络滞后,我无法对此进行优化。我希望日志包含另一个纯页面生成时间的字段,这样我就可以将其与$request_time进行比较。

我假设对于慢速连接,nginx会快速获取php-fpm输出,然后等待客户端下载。我可以用php测量生成时间,但如何将其放入nginx访问日志?

通过PHP将您想要nginx登录的数据放入一个头中。

header('RequestTime', 1234);

请nginx将头的值记录到nginx配置文件中。

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for" $upstream_http_requesttime';

请注意,该值也将通过Headers发送到客户端,因此不应在额外的Header中包含任何敏感性质的内容。