简体   繁体   English

如何在使用proxy_pass时在nginx上添加响应头?

[英]How to add a response header on nginx when using proxy_pass?

I want to add a custom header for the response received from the server behind nginx. 我想为从nginx后面的服务器收到的响应添加自定义标头。

While add_header works for nginx-processed responses, it does nothing when the proxy_pass is used. 虽然add_header适用于nginx处理的响应,但在使用proxy_pass时它不会执行任何操作。

add_header works as well with proxy_pass as without. add_header也可以和proxy_pass一样工作。 I just today set up a configuration where I've used exactly that directive. 我今天刚刚设置了一个配置,我已经完全使用了该指令。 I have to admit though that I've struggled as well setting this up without exactly recalling the reason, though. 我不得不承认,尽管如此,我还是在努力设置这个问题而没有完全回想起原因。

Right now I have a working configuration and it contains the following (among others): 现在我有一个工作配置,它包含以下(以及其他):

server {
    server_name  .myserver.com
    location / {
        proxy_pass  http://mybackend;
        add_header  X-Upstream  $upstream_addr;
    }
}

Before nginx 1.7.5 add_header worked only on successful responses, in contrast to the HttpHeadersMoreModule mentioned by Sebastian Goodman in his answer . 在nginx 1.7.5之前, add_header只对成功的响应起作用,与Sebastian Goodman在他的回答中提到的HttpHeadersMoreModule相反。

Since nginx 1.7.5 you can use the keyword always to include custom headers even in error responses. 从nginx 1.7.5 ,即使在错误响应中也可以使用关键字always来包含自定义标头。 For example: 例如:

add_header X-Upstream $upstream_addr always;

Limitation: You cannot override the server header value using add_header . 限制:您无法使用add_header覆盖server标头值。

There is a module called HttpHeadersMoreModule that gives you more control over headers. 有一个名为HttpHeadersMoreModule的模块,可以让您更好地控制标头。 It does not come with Nginx and requires additional installation. 它不附带Nginx并需要额外安装。 With it, you can do something like this: 有了它,你可以这样做:

location ... {
  more_set_headers "Server: my_server";
}

That will "set the Server output header to the custom value for any status code and any content type". 这将“将服务器输出标头设置为任何状态代码和任何内容类型的自定义值”。 It will replace headers that are already set or add them if unset. 它将替换已设置的标头,或者在未设置时添加它们。

As oliver writes: 正如奥利弗写道:

add_header works as well with proxy_pass as without. add_header也可以和proxy_pass一样工作。

However, as Shane writes, as of Nginx 1.7.5, you must pass always in order to get add_header to work for error responses, like so: 但是,正如Shane所写,从Nginx 1.7.5开始,您必须always传递以使add_header适用于错误响应,如下所示:

add_header  X-Upstream  $upstream_addr always;

You could try this solution : 你可以尝试这个解决方案:

In your location block when you use proxy_pass do something like this: 在使用proxy_pass location块中,执行以下操作:

location ... {

  add_header yourHeaderName yourValue;
  proxy_pass xxxx://xxx_my_proxy_addr_xxx;

  # Now use this solution:
  proxy_ignore_headers yourHeaderName // but set by proxy

  # Or if above didn't work maybe this:
  proxy_hide_header yourHeaderName // but set by proxy

}

I'm not sure would it be exactly what you need but try some manipulation of this method and maybe result will fit your problem. 我不确定它是否正是你需要的但是尝试一些这种方法的操作,结果可能适合你的问题。

Also you can use this combination: 您也可以使用此组合:

proxy_hide_header headerSetByProxy;
set $sent_http_header_set_by_proxy yourValue;

Hide response header and then add a new custom header value 隐藏响应标头,然后添加新的自定义标头值

Adding a header with add_header works fine with proxy pass, but if there is an existing header value in the response it will stack the values. 使用add_header添加标头与代理传递一起add_header工作,但如果响应中存在现有标头值,它将堆叠值。

If you want to set or replace a header value (for example replace the Access-Control-Allow-Origin header to match your client for allowing cross origin resource sharing) then you can do as follows: 如果要设置或替换标头值(例如,将Access-Control-Allow-Origin标头替换为与客户端匹配以允许跨源资源共享),则可以执行以下操作:

# 1. hide the Access-Control-Allow-Origin from the server response
proxy_hide_header Access-Control-Allow-Origin;
# 2. add a new custom header that allows all * origins instead
add_header Access-Control-Allow-Origin *;

So proxy_hide_header combined with add_header gives you the power to set/replace response header values. 所以proxy_hide_header结合add_header能够设置/替换响应报头值的权力。

Similar answer can be found here on ServerFault 类似的答案可以在ServerFault上找到

UPDATE: 更新:

Note: proxy_set_header is for setting request headers before the request is sent further, not for setting response headers (these configuration attributes for headers can be a bit confusing). 注意: proxy_set_header用于在进一步发送请求之前设置请求标头,而不是用于设置响应标头(标头的这些配置属性可能有点令人困惑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM