【问题标题】:nginx read custom response headernginx 读取自定义响应头
【发布时间】:2016-02-10 08:31:19
【问题描述】:

我正在尝试从自定义标头记录数据。回应:

Cache-Control:no-cache
Connection:keep-alive
Content-Type:application/json
Date:Mon, 09 Nov 2015 16:09:09 GMT
Server:nginx/1.9.4
Transfer-Encoding:chunked
X-Extended-Info:{"c":70}
X-Powered-By:PHP/5.6.12

在 php 脚本(Symfony2)中:

$response->headers->set('X-Extended-Info', json_encode($info))

我想从“X-Extended-Info”中写入日志数据。

Nginx 配置:

log_format main_log '$extended_info';
server {
    set $extended_info '-';
    if ($sent_http_x_extended_info != '') {
        set $extended_info $sent_http_x_extended_info;
    }
   ...
}

在日志中我只看到“-”。 我读过nginx - read custom header from upstream server,但这个解决方案在我的情况下不起作用(我尝试使用$upstream_http_,$http_)。

是否可以从 phpfpm 读取响应? 谢谢。

【问题讨论】:

  • 当我使用 $sent_http_connection 时,它可以工作。当我使用大多数其他标头时,$sent_http_ 为空。

标签: nginx


【解决方案1】:

if 指令在您的请求发送到后端之前起作用,因此当时没有$sent_http_... 变量。

您可以使用map 指令。

log_format main_log '$extended_info';

map $sent_http_x_extended_info $extended_info {
    default $sent_http_x_extended_info;
    "" "-";
}

【讨论】:

    【解决方案2】:

    我遇到了与 Niomin 相同的问题,预期的标题只是显示为“-”。我可以通过简单地将我的 fastcgi_pass 指令更改为 upstream 值来完成这项工作,然后我只是将它硬编码为如下所示的 unix 套接字:

    #fastcgi_pass unix:/home/phillip/php.sock; fastcgi_pass php_backend;

    我认为为upstream 服务器创建一个条目是记录酷$upstream_http_* 响应标头所必需的...下面是我创建的用于替换我的硬编码unix 套接字...

    upstream php_backend { server unix:/home/phillip/php.sock; }

    然后我编辑了 log_format 以包含 upstream_http_x_forwarded_by

    log_format main '$host - $remote_addr - [$time_local] "$request" ' '$status "X-Powered-By $upstream_http_x_powered_by "$http_referer" ''"$http_user_agent"';

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2017-12-10
      • 2020-04-01
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 2022-12-15
      相关资源
      最近更新 更多