TOC

Nginx proxy-set-header

线上环境 HTTP 服务有报获取不到 Host 信息。应该是域名,但是拿到的是 backend 这样的名字。

我第一反应肯定是 Nginx 的配置问题,我印象中 Nginx 给 upstream 转发的请求就会设置 Host 为 upstream 名称。

但是我检查了 Nginx 的配置 (nginx.conf), 在 http 这一层的配置中有 proxy_set_header Host $http_host;

不过搜索 proxy_set_header 的时候发现,在 server 这一层设置了 proxy_set_header X-Forwarded-For $remote_addr;, 这个头在 http 这一层也设置了,没必要在设置的。

我就想到,会不会下层设置头会覆盖上一层的所有头设置。

在 Nginx 文档 (proxy_set_header) 中查到:

Allows redefining or appending fields to the request header passed to the proxied server.
The value can contain text, variables, and their combinations.
These directives are inherited from the previous configuration level if and only if there are no proxy_set_header directives defined on the current level.

当本层没有 proxy_set_header 指令的时候,才会继承上一级的 proxy_set_header 指令。

将 server 层重复的这个设置拿掉,果然就好了。