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

dev*_*per 6 nginx

从根本上讲,在将proxy_pass存储在变量中之后,我很难找到一种方法来删除nginx中的响应头(如果proxy_pass)。proxy_hide_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 ""; 
  }   
 }
}
Run Code Online (Sandbox Code Playgroud)

我正在获得$ 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 
Run Code Online (Sandbox Code Playgroud)

有谁知道在将“ X-目标”存储并用于访问日志后如何删除它?我正在获取带有空值的“ $ destination_addr”。

谢谢

ana*_*six 9

可以proxy_hide_header用来删除从代理返回给 NGINX 的头文件,参见ngx_http_proxy_module 文档
$upstream_http_x_destination可用于日志记录,参见ngx_http_upstream_module 文档

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

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; 
    }   
  }
}
Run Code Online (Sandbox Code Playgroud)


pei*_*rms 3

我对此并不完全确定,但我认为只要您依赖该标头来记录日志,就需要将其设置为标头...否则,日志将无法使用它。

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