问题
我有一个rails 3.2.15和机架1.4.5安装在两个服务器上。第一个服务器是一个提供静态资产的nginx代理。第二个服务器是为rails应用提供服务的独角兽。
在Rails production.log中,我总是看到nginx地址(10.0.10.150),而不是我的客户机IP地址(10.0.10.62):
Started GET "/" for 10.0.10.150 at 2013-11-21 13:51:05 +0000我想把真正的客户端IP记录在日志中。
我们的设置
headers X-Forwarded-For和X-Real-IP是在nginx中正确设置的,我通过在config/environments/production.rb中设置config.action_dispatch.trusted_proxies = /^127\.0\.0\.1$/将10.0.10.62定义为不受信任的代理地址,这要归功于另一个回答。我可以检查它是否正常工作,因为我将它们记录在应用程序控制器中:
在app/controllers/application_controller.rb中
class ApplicationController < ActionController::Base
before_filter :log_ips
def log_ips
logger.info("request.ip = #{request.ip} and request.remote_ip = #{request.remote_ip}")
end
end在production.log中
request.ip = 10.0.10.150 and request.remote_ip = 10.0.10.62调查
在调查时,我发现Rails::Rack::Logger负责记录IP地址:
def started_request_message(request)
'Started %s "%s" for %s at %s' % [
request.request_method,
request.filtered_path,
request.ip,
Time.now.to_default_s ]
endrequest是ActionDispatch::Request的一个实例。它继承了定义IP地址计算方式的Rack::Request:
def trusted_proxy?(ip)
ip =~ /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|^::1$|^fd[0-9a-f]{2}:.+|^localhost$/i
end
def ip
remote_addrs = @env['REMOTE_ADDR'] ? @env['REMOTE_ADDR'].split(/[,\s]+/) : []
remote_addrs.reject! { |addr| trusted_proxy?(addr) }
return remote_addrs.first if remote_addrs.any?
forwarded_ips = @env['HTTP_X_FORWARDED_FOR'] ? @env['HTTP_X_FORWARDED_FOR'].strip.split(/[,\s]+/) : []
if client_ip = @env['HTTP_CLIENT_IP']
# If forwarded_ips doesn't include the client_ip, it might be an
# ip spoofing attempt, so we ignore HTTP_CLIENT_IP
return client_ip if forwarded_ips.include?(client_ip)
end
return forwarded_ips.reject { |ip| trusted_proxy?(ip) }.last || @env["REMOTE_ADDR"]
end转发的IP地址使用trusted_proxy?过滤。因为我们的nginx服务器使用的是公共IP地址,而不是私有IP地址,所以Rack::Request#ip认为它不是代理,而是试图进行ip欺骗的真正客户端IP。这就是我在日志中看到nginx IP地址的原因。
在日志摘要中,客户端和服务器具有IP地址10.0.10.x,因为我使用虚拟机来再现我们的生产环境。
我们目前的解决方案
为了避免这种行为,我编写了一个位于app/中间件/remote_ip_logger.rb中的小型Rack中间件:
class RemoteIpLogger
def initialize(app)
@app = app
end
def call(env)
remote_ip = env["action_dispatch.remote_ip"]
Rails.logger.info "Remote IP: #{remote_ip}" if remote_ip
@app.call(env)
end
end我把它插入到ActionDispatch::RemoteIp中间件之后
config.middleware.insert_after ActionDispatch::RemoteIp, "RemoteIpLogger"这样,我就可以在日志中看到真正的客户机IP:
Started GET "/" for 10.0.10.150 at 2013-11-21 13:59:06 +0000
Remote IP: 10.0.10.62我觉得这个解决方案有点不舒服。nginx+unicorn是rails应用程序的常见设置。如果我必须亲自记录客户端的IP,这意味着我错过了一些东西。是因为Nginx服务器在与rails服务器通信时使用公共IP地址吗?有办法自定义Rack::Request的Rack::Request方法吗?
编辑的:添加nginx配置和HTTP请求捕获
/etc/nginx/sites-enabled/site.example.com.conf
server {
server_name site.example.com;
listen 80;
location ^~ /assets/ {
root /home/deployer/site/shared;
expires 30d;
}
location / {
root /home/deployer/site/current/public;
try_files $uri @proxy;
}
location @proxy {
access_log /var/log/nginx/site.access.log combined_proxy;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_read_timeout 300;
proxy_pass http://rails.example.com:8080;
}
}Nginx服务器是10.0.10.150。Rails服务器是10.0.10.190。我的机器是10.0.10.62,当从我的机器执行curl http://10.0.10.150/时,tcpdump port 8080 -i eth0 -Aq -s 0 on rails服务器显示这些请求headers:
GET / HTTP/1.0
X-Forwarded-For: 10.0.10.62
X-Forwarded-Proto: http
Host: 10.0.10.150
Connection: close
User-Agent: curl/7.29.0
Accept: */*和rails日志/home/deployer/site/current/log/production.log (由自定义代码添加的远程IP和request.ip行):
Started GET "/" for 10.0.10.150 at 2013-11-22 08:01:17 +0000
Remote IP: 10.0.10.62
Processing by Devise::RegistrationsController#new as */*
request.ip = 10.0.10.150 and request.remote_ip = 10.0.10.62
Rendered devise/shared/_links.erb (0.1ms)
Rendered devise/registrations/new.html.erb within layouts/application (2.3ms)
Rendered layouts/_landing.html.erb (1.5ms)
Completed 200 OK in 8.9ms (Views: 7.5ms | ActiveRecord: 0.0ms)发布于 2013-11-25 13:57:55
在我看来,你目前的做法是唯一明智的方法。唯一缺少的步骤是覆盖env中的IP地址。
典型的REMOTE_ADDR很少持有正确的IP,如果您有任意数量的代理和负载平衡器,什么不是--在这方面,您并不是唯一的。每个文件都可能添加或更改与IP相关的远程报头。您不能假设这些字段中的每一个都必须对应于一个IP地址。有些人会将IP推送或取消到列表中。
只有一种方法可以确定哪个字段持有正确的值和方法,那就是潜入其中并查看。显然你已经这么做了。现在,只需使用Rack中间件覆盖具有正确值的env['REMOTE_ADDR']。让您没有编写日志或处理错误的IP地址的任何代码都是没有意义的,就像现在发生的那样。
(这是Ruby,您也可以猴子补丁Rack::Request,当然.)
要获得丰富多彩的阅读,说明异国情调的设置会在多大程度上破坏查找客户端真实IP地址的尝试,请参见对WordPress进行的无休止的讨论:
这是PHP,但是提出要点的要点同样适用于Ruby。(请注意,在我写这篇文章时,它们也没有得到解决,而且它们已经存在了很长时间。)
发布于 2014-11-06 20:27:28
这似乎对我有好处。(在nginx配置中设置)
proxy_set_header CLIENT_IP $remote_addr;发布于 2016-10-05 22:11:04
我遇到了同样的问题,我们的一部分web客户端在我们的专用网络上访问我们的rails应用程序(Rails 4.2.7),我们得到了错误的IP报告。所以,我想我应该补充一下我们解决问题的方法。
我发现Rails问题5223提供了比双重登录IP更好的解决方法,就像问题一样。因此,我们猴子修补程序Rack将私有网络从可信代理列表中删除,如下所示:
module Rack
class Request
def trusted_proxy?(ip)
ip =~ /^127\.0\.0\.1$/
end
end
end这将解决记录错误IP的控制器,即修复的另一半,以确保正确处理request.remote_ip。为此,请将以下内容添加到配置/环境/production.rb中:
config.action_dispatch.trusted_proxies = [IPAddr.new('127.0.0.1')] https://stackoverflow.com/questions/20124292
复制相似问题