当使用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
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。