参考: http://gong1208.iteye.com/blog/1559835
使用nginx 做反向代理之后,request.remote_ip (rails方法) 是无法获取该IP的真实来源的.
所以,一个思路,就是,在转发的时候,把真是的ip放到nginx 转发后的request header中.
nginx 配置:
location / {
proxy_pass http://shang_yun_yi_jia_servers;
proxy_redirect default;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
然后,rails中:
puts request.headers['HTTP_X_REAL_IP'] (Rails4 开始, HEADER中的key, 要加上 HTTP_前缀