您的位置 首页 > 腾讯云社区

nginx proxy_set_header配置覆盖---fanspring

问题

当使用nginx反代时候我们经常会使用proxy_set_header来设置http头,如下面是一个常用的配置。

server { listen 8080; server_name _; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; location ^~ /test/ { proxy_set_header host $host:$server_port; proxy_pass http://127.0.0.1:8888; } }

然而,你可能会发现,使用上面配置后,在服务端127.0.0.1:8888的抓包查看http header,发现只有host字段,没有X-Real-IP字段。

原因

其实,官方文档已经说明的很明确了

以下是关于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 level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:

意思是location中只有在当前并没有proxy_set_header的配置时才会继承上一层的proxy_set_header的设置。

参考

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

---来自腾讯云社区的---fanspring

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: