Apache + HAProxy 一台服务器(A)
Rails App 两台服务器(B,C) version=2.0.4
前段时间突然发现production.log中只能显示haproxy的IP.而不能显示真正的client ip
但在/etc/haproxy.cfg中已经注释了
初步怀疑是HAProxy到mongrel的问题。经过在mongrel中debug,发现是rails code的问题。
rails v2.0.2升级到rails v2.0.4,加强了对IP的安全检查。
在application.rb中overwrite rails code.增加
Production.log中能够显示client的真实IP
Rails App 两台服务器(B,C) version=2.0.4
前段时间突然发现production.log中只能显示haproxy的IP.而不能显示真正的client ip
但在/etc/haproxy.cfg中已经注释了
#option forwardfor # enable insert of X-Forwarded-For headers初步怀疑是HAProxy到mongrel的问题。经过在mongrel中debug,发现是rails code的问题。
rails v2.0.2升级到rails v2.0.4,加强了对IP的安全检查。
在application.rb中overwrite rails code.增加
#Overrides TRUSTED_PROXIES to allow the logging of the HTTP_X_FORWARDED_FOR ip in the logs (i.e. the client's ip):
ActionController::AbstractRequest.const_set("TRUSTED_PROXIES", /^unknown$|^111\.111\.111\.11|22$|^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i)Production.log中能够显示client的真实IP

本文介绍了解决Rails 2.0.4应用程序中无法正确记录客户端真实IP地址的问题。通过调整Rails配置,允许记录HTTP_X_FORWARDED_FOR头部中的IP,确保生产环境中日志能准确反映客户端IP。
240

被折叠的 条评论
为什么被折叠?



