如何在调用PHP之前在Apache中设置REMOTE_ADDR [英] How to set REMOTE_ADDR in apache before php is invoked

查看:438
本文介绍了如何在调用PHP之前在Apache中设置REMOTE_ADDR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用nginx设置的网站,作为运行php的Apache 2.2的反向代理.从apache和php的角度来看,所有请求的IP地址都是nginx服务器.我希望php能看到与nginx相同的远程IP.

I have a website set up with nginx acting as a reverse proxy to apache 2.2, which is running php. From apache and php's perspective the IP address of all requests is the nginx server. I'd like php to see the same remote IP that nginx sees.

Nginx设置标头X-Real-IP,其中包含nginx看到的远程IP.我试图在apache conf中做这样的事情:

Nginx sets a header X-Real-IP which contains the remote IP that nginx sees. I tried doing something like this in the apache conf:

SetEnvIf ^X-Real-IP$ "(.+)" REMOTE_ADDR=$1

我希望我可以设置REMOTE_ADDR环境变量,并且当php最终被调用时,它将看到nginx看到的远程IP.我认为php代码正在执行此操作:

My hope was that I could set the REMOTE_ADDR environment variable and when php finally gets invoked, it would see the remote IP that nginx sees. I think the php code is doing this:

$_SERVER['REMOTE_ADDR']

无论如何,这是行不通的.有任何想法吗?您不能在apache配置文件中设置REMOTE_ADDR吗?谢谢.

Anyway, this isn't working. Any ideas? Can you not set REMOTE_ADDR in the apache config file? Thanks.

推荐答案

不确定REMOTE_ADDR是否可以通过这种方式更改...

Not sure whether REMOTE_ADDR can be changed that way...


实际上,您可能需要安装/启用另一个Apache模块,例如 mod_rpaf (报价):


Actually, you might need to install / enable another Apache module, like mod_rpaf (quoting) :

它更改了远程地址 客户端对其他Apache模块可见 当两个条件都满足时.
第一个条件是遥控器 客户实际上是一个代理 在httpd.conf中定义.
第二,如果 有一个传入的X-Forwarded-For 标头和代理在它的列表中 最后的IP的已知代理的数量 从传入的X-Forwarded-For 标头并更改远程地址 客户在请求中 结构体.
这还需要 传入的X-Host标头并更新 虚拟主机设置相应.
对于Apache2 mod_proxy,它需要 X-Forwared-Host标头并更新 虚拟主机

It changes the remote address of the client visible to other Apache modules when two conditions are satisfied.
First condition is that the remote client is actually a proxy that is defined in httpd.conf.
Secondly if there is an incoming X-Forwarded-For header and the proxy is in it's list of known proxies it takes the last IP from the incoming X-Forwarded-For header and changes the remote address of the client in the request structure.
It also takes the incoming X-Host header and updates the virtualhost settings accordingly.
For Apache2 mod_proxy it takes the X-Forwared-Host header and updates the virtualhosts

这是关于此的博客文章: Apache的Nginx代理-使用mod_praf访问远程主机IP地址

Here's a blog-post about that : Nginx proxy to Apache - access remote host IP address using mod_praf

更新:原始链接目前无法正常工作,但也可以作为debian软件包使用:apt-get install libapache2-mod-rpaf

Update: original link not working right now, but it is available also as a debian package: apt-get install libapache2-mod-rpaf

这篇关于如何在调用PHP之前在Apache中设置REMOTE_ADDR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆