nginx - 在proxy_pass的情况下存储其值后如何删除nginx中的响应头

标签 nginx

从根本上说,在将 proxy_pass 存储在变量中后,我很难找到一种方法来删除 nginx 中的响应 header ,以防万一。
proxy_hide_header 不允许我存储该值。

换句话说:

我正在尝试访问自定义响应 header ,将其保存到一个变量中,然后将其删除,以便它不会传播到客户端。然后在访问日志中使用该变量。
不幸的是,以下似乎不起作用:

http {

log_format main '$remote_addr $http_host $destination_addr [$time_local]   
"$request" $status';
access_log /opt/logs/access.log main;

server { 
 listen 142.133.151.129:8090 default; 

 ##This is my internal variable for the response header field 
 set $destination_addr "-"; 

 location / { 
   proxy_pass http://my_upstream_server; 

   #store the response header field
   set $destination_addr $sent_http_x_destination; 

   #now get rid of this response header field
   set $sent_http_x_destination ""; 
  }   
 }
}

我得到 $sent_http_x_destination 的空值。

这是 curl 请求和响应:
# curl -Ov http://142.133.151.129:8090/ao3/vod/soccer/worldcup2014/final1

< HTTP/1.1 206 Partial Content
< Server: openresty/1.9.3.1
< Date: Tue, 16 Feb 2016 22:25:32 GMT
< Content-Type: application/octet-stream
< Content-Length: 99990100
< Connection: keep-alive
< X-Destination: 142.133.151.94 

有谁知道如何在存储并用于访问日志后删除“X-Destination”?我得到了空值的“$destination_addr”。

谢谢

最佳答案

可以用proxy_hide_header要从代理中删除返回给 NGINX 的 header ,请参阅 ngx_http_proxy_module docs$upstream_http_x_destination可用于日志记录,参见 ngx_http_upstream_module docs

重写您的配置将如下所示:

http {
  log_format main '$remote_addr $http_host $upstream_http_x_destination '
                  '[$time_local] "$request" $status';
  access_log /opt/logs/access.log main;
  server { 
    listen 142.133.151.129:8090 default; 
    location / {
      proxy_hide_header x_destination;
      proxy_pass http://my_upstream_server; 
    }   
  }
}

关于nginx - 在proxy_pass的情况下存储其值后如何删除nginx中的响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35493418/

相关文章:

python - http客户端需要访问nginx两次才能加载css文件

php - 在 nginx ubuntu 中设置 laravel 时出现给定错误的可能原因是什么?

php - 如何正确链接 2 个容器?

nginx - 如何在 nginx 变量中转义 $

r - Shiny可以判断登录nginx反向代理的用途

nginx - 为什么将域连接到 "Shopify"要求 CNAME 和 A 记录?

tomcat - NGINX 错误 : upstream sent invalid chunked response while reading upstream

python - 如何配置 nginx 为另一个域提供特定的 django url 路径

ruby-on-rails - Nginx ssl_protocol 设置不起作用

c++ - 我可以在 nginx + spawn-fcgi 中保持 MySQL 连接永远打开吗?