apache ProxyPass:如何保留原始IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/760283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
apache ProxyPass: how to preserve original IP address
提问by ashweta
We are using ProxyPass to redirect all "/r" requests to jboss on port 18080 as follows:
我们使用 ProxyPass 将所有“/r”请求重定向到端口 18080 上的 jboss,如下所示:
ProxyPreserveHost on
ProxyPass /r http://localhost:18080/redirectService/
ProxyPassReverse /r http://localhost:18080/redirectService/
But, that causes the IP address logged in jboss's access log as "127.0.0.1". Does somebody know how can we preserve the original IP from where the request came in HttpServletRequest? We want to acesss it from jboss servlet request in doGet()
但是,这会导致 jboss 的访问日志中记录的 IP 地址为“127.0.0.1”。有人知道我们如何保留来自 HttpServletRequest 请求的原始 IP 吗?我们想从 doGet() 中的 jboss servlet 请求访问它
采纳答案by andri
You can get the original host from X-Forwarded-Forheader field.
您可以从X-Forwarded-For标头字段中获取原始主机。
回答by Steffen
The answer of JasonW is fine. But since apache httpd 2.4.6 there is a alternative: mod_remoteip
JasonW 的回答很好。但由于 apache httpd 2.4.6 有一个替代方案:mod_remoteip
All what you must do is:
您必须做的就是:
- May be you must install the mod_remoteip package
Enable the module:
LoadModule remoteip_module modules/mod_remoteip.soAdd the following to your apache httpd config. Note that you must add this line notinto the configuration of the proxy server. You must add this to the configuration of the proxy target httpd server(the server behind the proxy):
RemoteIPHeader X-Forwarded-For
- 可能是你必须安装 mod_remoteip 包
启用模块:
LoadModule remoteip_module modules/mod_remoteip.so将以下内容添加到您的 apache httpd 配置中。请注意,您必须添加此行不为代理服务器的配置。您必须将此添加到代理目标 httpd 服务器(代理背后的服务器)的配置中:
RemoteIPHeader X-Forwarded-For
See at http://httpd.apache.org/docs/trunk/mod/mod_remoteip.htmlfor more informations and more options.
有关更多信息和更多选项,请参见http://httpd.apache.org/docs/trunk/mod/mod_remoteip.html。
回答by JasonW
This has a more elegant explanation and more than one possible solutions. http://kasunh.wordpress.com/2011/10/11/preserving-remote-iphost-while-proxying/
这有一个更优雅的解释和不止一种可能的解决方案。http://kasunh.wordpress.com/2011/10/11/preserving-remote-iphost-while-proxying/
The post describes how to use one popular and one lesser known Apache modules to preserve host/ip while in a setup involving proxying.
这篇文章描述了如何在涉及代理的设置中使用一个流行的和一个鲜为人知的 Apache 模块来保留主机/IP。
Use mod_rpaf module, install and enable it in the backend server and add following directives in the module's configuration. RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1
使用 mod_rpaf 模块,在后端服务器中安装并启用它,并在模块的配置中添加以下指令。RPAFenable 在
RPAFsethostname 上
RPAFproxy_ips 127.0.0.1
(2017 edit) Current location of mod_rpaf: https://github.com/gnif/mod_rpaf
(2017 编辑)mod_rpaf 的当前位置:https: //github.com/gnif/mod_rpaf
回答by Jason Fritcher
If you have the capability to do so, I would recommend using either mod-jkor mod-proxy-ajpto pass requests from Apache to JBoss. The AJP protocol is much more efficient compared to using HTTP proxy requests and as a benefit, JBoss will see the request as coming from the original client and not Apache.
如果您有能力这样做,我建议您使用mod-jk或mod-proxy-ajp将请求从 Apache 传递到 JBoss。与使用 HTTP 代理请求相比,AJP 协议要高效得多,而且作为一个好处,JBoss 会将请求视为来自原始客户端而不是 Apache。
回答by Tarun Gupta
If you are using Apache reverse proxy for serving an app running on a localhost port you must add a location to your vhost.
如果您使用 Apache 反向代理为在本地主机端口上运行的应用程序提供服务,您必须向您的虚拟主机添加一个位置。
<Location />
ProxyPass http://localhost:1339/ retry=0
ProxyPassReverse http://localhost:1339/
ProxyPreserveHost On
ProxyErrorOverride Off
</Location>
To get the IP address have following options
要获取 IP 地址有以下选项
console.log(">>>", req.ip);// this works fine for me returned a valid ip address
console.log(">>>", req.headers['x-forwarded-for'] );// returned a valid IP address
console.log(">>>", req.headers['X-Real-IP'] ); // did not work returned undefined
console.log(">>>", req.connection.remoteAddress );// returned the loopback IP address
So either use req.ip or req.headers['x-forwarded-for']
所以要么使用 req.ip 要么 req.headers['x-forwarded-for']

