ruby-on-rails C 当从私有NIC访问时,Rails显示IP为127.0.0.1,但Nginx显示正确的IP
发布时间:2023-12-18 23:13:01 所属栏目:Nginx 来源:DaWei
导读: 我们正在Unicorn Nginx上运行Rails应用程序.服务器有两个我们使用的网卡. eth0处理公共互联网的请求,eth2处理来自我们私人网络的请求.
当通过eth0发出请求时,nginx日志显示公网IP,而Rails日
当通过eth0发出请求时,nginx日志显示公网IP,而Rails日
|
我们正在Unicorn Nginx上运行Rails应用程序.服务器有两个我们使用的网卡. 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; }有任何想法吗? 最佳答案 问题是Rails认为任何192.168.x.x地址是一个私有地址,因此从X-Forwarded_For标题中删除它们. # 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请参阅相关的Rails源here和here. 一个解决方案是将其添加到你的config / application.rb中: config.action_dispatch.trusted_proxies = /^127.0.0.1$/ # localhost这样,本地网络上的IP将不会被’127.0.0.1’所取代. (编辑:吉安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- php – Nginx在Windows上指向Docker的错误目录
- nginx-我真的需要Web服务器以及API-微服务架构中的网关
- 身份验证 C nginx auth_basic时间限制
- 如何实现Nginx不区分大小写的目录位置重定向301
- ruby-on-rails-乘客nginx:在子目录中托管Rails应用程序
- 从Nginx中的Set-Cookie标头登录值
- ruby-on-rails-capistrano部署后ec2服务器中缺少puma.sock
- apache C NGINX配置与Socket.IO一起使用
- 如何在Apache服务器(和其他服务器)上的PHP中检测对.htacces
- ruby-on-rails-如何配置Nginx代理到Rails应用程序?这样我就
推荐文章
站长推荐
- Nginx忽略了我的标题
- java – nginx:当使用nginx作为反向代理时,是否
- node.js-带有Nodejs中的Push的Nodejs http2(ngin
- 具有可变参数顺序的Nginx缓存
- php-Laravel Forge Nginx Config for SSL
- php5-fpm nginx google bot =连接由同行重置
- Nginx反向代理Websocket身份验证 – HTTP 403
- ruby-on-rails – 更改Nginx的乘客默认错误页面
- 如何为Nginx设置client_max_body_size动态
- nginx C 如何部署我的Angular 2 Typescript Webp
