2011-11-24 68 views
2

在nginx中,您如何设置您的proxy_pass,以便发送到正确的客户端远程IP地址?目前,只有服务器的IP显示。我知道这样做的正确方法是

proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $remote_addr; 

但是,另一方面,我有一个不是由我写的支持系统。我不想更改所有代码以符合X-Forwarded-For参数。

如何告诉nginx发送$remote_addr的格式,以便可以用$_SERVER['REMOTE_ADDR']读取?

+0

哪个服务器软件作为后端运行?如果它是apache,请尝试安装mod_rpaf。 –

+0

在代理服务器上它是nginx,安装支持系统的另一台服务器只是一个托管的网站空间。 –

回答

0

proxy_set_header REMOTE_ADDR $remote_addr;不nginx的proxy_pass(link)工作

所以,你必须准备一些中间件到应用程序。 例如,伪代码:

class RemoteAddrMiddleware(request) 
    def process_request(request): 
     if request['REMOTE_ADDR'].blank? 
      request['REMOTE_ADDR'] = request['X-Forwarded-For'] 
     end 
    end 
end 
1

如果代理到Apache 2.4服务器,您可以使用该模块mod_remoteip。它将头的X转发,对于一个远程地址头感谢“翻译”这些指令,下面一个例子:

#### Set that the Remote IP comes from the header X-Forwared-for 
RemoteIPHeader X-Forwarded-For 
#### Trust the proxy at localhost 
RemoteIPTrustedProxy 127.0.0.1 
#### Trust the proxy with the address range from 192.168.15.0 to 192.168.15.255 
RemoteIPTrustedProxy 192.168.15.0/24 
0

在Apache 2.2的,你也可以使用中的libapache2-MOD-rpaf。

相关问题