从根本上说,在将 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/