我们正在 Unicorn + Nginx 上运行 Rails 应用程序。服务器有两个我们使用的 NIC。eth0
处理对公共互联网的eth2
请求,并处理来自我们专用网络的请求。
当请求通过eth0
时,nginx 日志会显示公共 IP,Rails 日志也会显示此 IP。但是,当请求通过eth2
时,nginx 日志会正确显示私有 IP(例如192.168.5.134
),但 Rails 日志会显示127.0.0.1
.
因此,似乎公共请求正确设置eth0
了它们的X-Forwarded-For
标头,但对于eth2
.
我们的 nginx 配置非常基本:
upstream example.com {
server unix://var/www/example.com/shared/sockets/unicorn.socket fail_timeout=0;
}
...
server {
listen 443 ssl;
...
location @example.com {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real_IP $remote_Addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if ($host ~* "^(.+)\.example.com$") {
set $subdomain $1;
}
proxy_pass http://example.com;
}
有任何想法吗?