簡體   English   中英

用 Rails 和 Nginx 獲取客戶端的真實 IP 地址?

[英]Get the real IP address of client with Rails and Nginx?

我的服務器沒有公開的 IP 地址,所以不知道如何獲取真實客戶端的 IP 地址。

這是我的 nginx 的配置:

location / {
    proxy_pass http://domain1;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP     $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}

在我的 Rails 應用程序的 controller 中, request.iprequest.remote_ip返回我服務器的網關地址。

我怎樣才能得到客戶真正的 IP?

如何從 Rails 請求中獲取 X-Forwarded-For 值?

Rails 應該會自動為我們做這件事,但它似乎被當前的 3.x 打破了

我正在使用這個:

def ip() request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip end

你應該得到 header 值 X-forwarded-for

http://en.wikipedia.org/wiki/X-Forwarded-For

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM