PHP $_SERVER[‘SERVER_ADDR’] 变量始终返回 127.0.0.1
我们有多个负载平衡的网络服务器机器运行相同的 PHP Web 应用程序 (LAMP) 和我想在每台服务器上运行稍微不同的代码(出于测试目的)。我希望使用 $_SERVER['SERVER_ADDR']
超级全局来做这样的事情:
if ($_SERVER['SERVER_ADDR'] == 'XXX.XXX.XXX.XXX') {
echo "Do one thing";
} elseif ($_SERVER['SERVER_ADDR'] == 'YYY.YYY.YYY.YYY') {
echo "Do something else";
}
不幸的是,这不起作用,因为两台机器都在设置 $_SERVER['SERVER_ADDR']
到“127.0.0.1”。如何配置它们,以便将 $_SERVER['SERVER_ADDR']
设置为其公共 IP 地址?
我猜测这个问题可能与 /etc/hosts
有关,因此作为参考,它目前看起来像这样:
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
XXX.XX.XX.XX blahblah
更新...
哎呀!我忽略了考虑 Web 服务器前面的 nginx 反向代理。由于 nginx conf 中的以下行,这些 Web 服务器的所有流量均来自 nginx:
location / {
root /var/www/staging/current;
proxy_pass http://localhost:8880;
}
We have multiple load-balanced webserver machines running the same PHP webapp (LAMP) and I'd like to run slightly different code on each server (for testing purposes). I was hoping to use the $_SERVER['SERVER_ADDR']
super global to do something like this:
if ($_SERVER['SERVER_ADDR'] == 'XXX.XXX.XXX.XXX') {
echo "Do one thing";
} elseif ($_SERVER['SERVER_ADDR'] == 'YYY.YYY.YYY.YYY') {
echo "Do something else";
}
Unfortunately, this doesn't work because both machines are setting $_SERVER['SERVER_ADDR']
to '127.0.0.1'. How can I configure them so that $_SERVER['SERVER_ADDR']
is set to their public IP address?
I'm guessing the issue may be something to do with /etc/hosts
so for refererence it currently looks like this:
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
XXX.XX.XX.XX blahblah
Update...
Oops! I neglected to consider the nginx reverse proxy in front of the web servers. All the traffic to those web servers arrives from nginx due to the following line in the nginx conf:
location / {
root /var/www/staging/current;
proxy_pass http://localhost:8880;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当然很简单
Surely it's as simple as
它可能涉及更改负载平衡器连接到服务器的方式。我不知道这是什么软件。
您可能最好根据机器之间变化的其他一些因素进行切换。一个不错的选择是主机名:
It would probably involve changing how the load-balancer connects to the server. I don't know what software this is.
You might be better off switching based on some other factor that changes between the machines. A good bet would be the hostname:
反转 /etc/hosts 中的行
应该可以
reverse rows in /etc/hosts
should work
用这个更准确!
Use this more accurate!
为了解决我的问题,我有 3 个想法:
proxy_pass
以转到XXX.XXX.XXX.XXX:8880
和YYY.YYY.YYY.YYY:8880
> 而不是localhost
?To fix my issue, I have 3 ideas:
proxy_pass
on each server to go toXXX.XXX.XXX.XXX:8880
andYYY.YYY.YYY.YYY:8880
rather thanlocalhost
?你真的应该有一些服务器特定的配置被加载,里面有一个服务器ID。
每个系统的行为都会有所不同,并且对于 IP 地址、主机名来说肯定非常容易出错。通常服务器上有很多应用程序,从一天到另一天,它可能不再工作,并且您将很难进行调试(例如,some1 geths 设置反向条目是个好主意,这样邮件就不会再收到垃圾邮件了吗? )
you should really have som server specific config that gets loaded and a server id inside.
every system will behave differently and going for ip adresses, hostname is definately very error vulnerable. usually there are many applications on the server and from one day to another it might not work any longer and you will have a hard time debugging (for example some1 geths the nice idea to set up a reverse entry so mails dont get spammed any more?)
在 Apache 2.4 及更高版本中 $_SERVER['SERVER_ADDR'] 如果您从本地主机调用服务器,它将返回类似 ::1 而不是 127.0.0.1
要解决这个问题,请转到您的 Apache conf 文件并更改
为
最后确保您的服务器不在反向代理后面
In Apache 2.4 and later $_SERVER['SERVER_ADDR'] if you are calling the server from the localhost it will return something like ::1 instead of 127.0.0.1
To solve that go to your Apache conf file and change
to
Finally make sure that your server is not behind a reverse proxy