2011-11-24 71 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。

相關問題