Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx 在使用proxy_pass时,如何在存储了响应头的值后删除该响应头_Nginx - Fatal编程技术网

Nginx 在使用proxy_pass时,如何在存储了响应头的值后删除该响应头

Nginx 在使用proxy_pass时,如何在存储了响应头的值后删除该响应头,nginx,Nginx,从根本上说,我很难找到一种方法来删除nginx中的响应头,以防在将响应头存储在变量中之后出现proxy_pass。 proxy\u hide\u标头不允许我存储值 换言之: 我试图访问自定义响应头,将其保存到变量中,然后将其删除,这样它就不会传播到客户端。然后在访问日志中使用该变量。 不幸的是,以下方法似乎不起作用: http { log_format main '$remote_addr $http_host $destination_addr [$time_local] "$requ

从根本上说,我很难找到一种方法来删除nginx中的响应头,以防在将响应头存储在变量中之后出现proxy_pass。 proxy\u hide\u标头不允许我存储值

换言之:

我试图访问自定义响应头,将其保存到变量中,然后将其删除,这样它就不会传播到客户端。然后在访问日志中使用该变量。 不幸的是,以下方法似乎不起作用:

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\u http\u x\u 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 
#curl-Ovhttp://142.133.151.129:8090/ao3/vod/soccer/worldcup2014/final1
有人知道在存储并用于访问日志后如何删除“X-Destination”吗?我得到的“$destination\u addr”值为空


谢谢

我不能完全确定这一点,但我认为,只要日志依赖于该标头,就需要将其设置为标头。。。否则,日志将无法使用它


话虽如此,您可以尝试看看它是否有效。

我能看到的唯一方法是创建一个lua脚本,该脚本将使用http resty重新实现代理传递,将头保存在nginx变量中,并在返回之前将其删除。保存的nginx变量随后将用于访问日志。 这是我的答案。 我希望nginx提供一种更简单的方法来实现这一点

请让我知道你对这个答案的评论。
感谢您对本主题的所有投入

可以使用
proxy\u hide\u header
从代理中删除返回给NGINX的头,请参见
$upstream\u http\u x\u destination
可用于日志记录,请参阅

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

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; 
    }   
  }
}

我已经尝试了代理\u隐藏\u标题。它不允许我在访问日志中使用header字段的值。头球立即消失。这就是为什么我发布了我的问题:“在proxy_pass服务器中,我们如何在访问日志中使用响应头并在之后将其删除?”正如我所说,如果您计划在日志中使用它,您不能让它消失。日志依赖于这些头,当它们被写入日志时,这些头“已经发送”给用户并按预期工作。它从响应中删除头,但变量
$upstream\u http\u my\u custom\u头
写入日志中。