请教一个Nginx proxy_set_header作用域的问题

516 views
Skip to first unread message

colinzhouyj

unread,
Dec 22, 2013, 9:29:09 PM12/22/13
to sh...@googlegroups.com

之前的配置是这样的

location / {

                proxy_pass              http://backend_server;

                proxy_set_header        Host $host;

                proxy_set_header        X-Real-IP $remote_addr;

                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

                ## for keepalive set

                proxy_http_version      1.1;

                proxy_set_header        Connection "";

                }

 

后来因为location比较多,单个nginx里面server也不少,觉得每个地方都写很麻烦,就把

proxy_set_header        Host $host;

proxy_set_header        X-Real-IP $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

3行写到了 http标签里。但是后端server获取到的IP就是nginxIP了,配置到location里的时候是能正确获取客户端IP的。

请教一下,这个是作用域的问题吗?

 

 

Harold.Miao

unread,
Dec 22, 2013, 10:04:52 PM12/22/13
to sh...@googlegroups.com
是的  不能写在http里面     如果你嫌到处引用麻烦   可以用include指令

在 2013年12月23日星期一,colinzhouyj 写道:
--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。


--

Best Regards,
Harold Miao

Yiling Cao

unread,
Dec 23, 2013, 1:59:48 AM12/23/13
to sh...@googlegroups.com
就你规定的location 下吧。 我记得是的。你测下


2013/12/23 Harold.Miao <miaoh...@gmail.com>

colin

unread,
Dec 23, 2013, 8:28:52 PM12/23/13
to sh...@googlegroups.com
经过测试,这个设置是这样的。
如果在http里面设置了proxy_set_header 参数,那么server 或者location里设置 任意一条proxy_set_header记录,都会重写http设置的所有proxy_set_header记录。
所以,如果在http里面设置了proxy_set_header参数的话,server里和location里都不能有这个参数。
同理,如果在server里设置了这个参数,location里没有设置的话,location会使用到server里的设置值。当然,如果location设置了任意一个proxy_set_header记录,server
里的所有设置在location里也不生效。
 
我的理解是,proxy_set_header参数相互之间的设置是不影响的,但是对nginx来说,每个设置的变量都是唯一的,也就是说,在http里设置了proxy_set_header,不管设置 了多少个
到了server 或是下一级容器内,只要有设置proxy_set_header,它的值都应该是被重新定义的。
 
 

在 2013年12月23日星期一UTC+8上午10时29分09秒,colin写道:

Perry

unread,
Dec 24, 2013, 2:22:28 AM12/24/13
to sh...@googlegroups.com
感觉你钻牛角尖了,你可以把通用的proxy配置放到单独的文件里,然后在location里include就可以避免重复了


2013/12/24 colin <colin...@gmail.com>

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。



--
Perry | 彭琪

colinzhouyj

unread,
Dec 24, 2013, 10:05:12 PM12/24/13
to sh...@googlegroups.com

include确实是一个好方法。

还有一点想确认一下,原本的想法是在http设置了默认的proxy_set_header,location里如果修改的话,会把对应的key值覆盖,其他key值还是取http里面的设置,最后发现是不行的。

如果用include的方法,在location里重复设置一次,会引起重叠吗,就是出现2key值在http head

 

发件人: sh...@googlegroups.com [mailto:sh...@googlegroups.com] 代表 Perry
发送时间: 20131224 15:22
收件人: sh...@googlegroups.com
主题: Re: [shlug] Re: 请教一个Nginx proxy_set_header作用域的问题

您收到此邮件是因为您订阅了 Google 网上论坛“Shanghai Linux User Group”中的主题。
要退订此主题,请访问 https://groups.google.com/d/topic/shlug/0UGgkyWMLk4/unsubscribe
要退订此论坛及其所有主题,请发送电子邮件到 shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out

李晓岚

unread,
Dec 25, 2013, 2:48:49 AM12/25/13
to sh...@googlegroups.com
按照文档,要么全部继承,要么全部替换,不会混用两个level的设置。
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
syntax: proxy_set_header field value;
default:

proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
context: httpserverlocation

Allows redefining or appending fields to the request header passed to the proxied server. A 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:


--
不学无术


2013/12/25 colinzhouyj <colin...@gmail.com>

colinzhouyj

unread,
Dec 26, 2013, 12:14:15 AM12/26/13
to sh...@googlegroups.com

就是这个。

 

发件人: sh...@googlegroups.com [mailto:sh...@googlegroups.com] 代表 李晓岚
发送时间: 20131225 15:49
收件人: sh...@googlegroups.com
主题: Re: 答复: [shlug] Re: 请教一个Nginx proxy_set_header作用域的问题

Reply all
Reply to author
Forward
0 new messages