大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 当从私有NIC访问时,Rails显示IP为127.0.0.1,但Nginx显示正确的IP.公网IP转发正常,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当通过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 $scheR_838_11845@e; 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; }
有任何想法吗?
# IP addresses that are "trusted proxies" that can be Stripped from # the comma-delimited list in the X-ForWARDed-For header. See also: # http://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces TRUSTED_PROXIES = %r{ ^127\.0\.0\.1$ | # localhost ^(10 | # private ip 10.x.x.x 172\.(1[6-9]|2[0-9]|3[0-1]) | # private ip in the range 172.16.0.0 .. 172.31.255.255 192\.168 # private ip 192.168.x.x )\. }x
一个解决方案是将其添加到你的config / application.rb中:
config.action_dispatch.trusted_proxies = /^127\.0\.0\.1$/ # localhost
这样,本地网络上的IP将不会被’127.0.0.1’所取代.
以上是大佬教程为你收集整理的ruby-on-rails – 当从私有NIC访问时,Rails显示IP为127.0.0.1,但Nginx显示正确的IP.公网IP转发正常全部内容,希望文章能够帮你解决ruby-on-rails – 当从私有NIC访问时,Rails显示IP为127.0.0.1,但Nginx显示正确的IP.公网IP转发正常所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。