PHP $_SERVER[‘SERVER_ADDR’] 变量始终返回 127.0.0.1

发布于 2024-09-09 06:40:58 字数 1134 浏览 3 评论 0原文

我们有多个负载平衡的网络服务器机器运行相同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蓝颜夕 2024-09-16 06:40:58

当然很简单

$ip = getHostByName(php_uname('n')); 
echo $ip;

Surely it's as simple as

$ip = getHostByName(php_uname('n')); 
echo $ip;
随心而道 2024-09-16 06:40:58

它可能涉及更改负载平衡器连接到服务器的方式。我不知道这是什么软件。

您可能最好根据机器之间变化的其他一些因素进行切换。一个不错的选择是主机名:

$host= php_uname('n');
switch($host) {
    case 'webserver1':
        ...do one thing...
        break;
    case 'webserver2':
        ...do another thing...
        break;
    default:
        die('No configuration for unknown host '.$host);
}

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:

$host= php_uname('n');
switch($host) {
    case 'webserver1':
        ...do one thing...
        break;
    case 'webserver2':
        ...do another thing...
        break;
    default:
        die('No configuration for unknown host '.$host);
}
丘比特射中我 2024-09-16 06:40:58

反转 /etc/hosts 中的行

XXX.XX.XX.XX    blahblah
127.0.0.1       localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

应该可以

reverse rows in /etc/hosts

XXX.XX.XX.XX    blahblah
127.0.0.1       localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

should work

蛮可爱 2024-09-16 06:40:58

用这个更准确!

echo getHostByName($_SERVER[HTTP_HOST]);

Use this more accurate!

echo getHostByName($_SERVER[HTTP_HOST]);
メ斷腸人バ 2024-09-16 06:40:58

为了解决我的问题,我有 3 个想法:

  1. 我可以将每台服务器的 IP 地址硬编码到每台服务器上的配置文件中的 PHP 变量中。
  2. 我可能会使用 Apache 的反向代理添加转发模块 (mod_rpaf) 。
  3. 我可能会更改每个服务器上的 proxy_pass 以转到 XXX.XXX.XXX.XXX:8880YYY.YYY.YYY.YYY:8880 > 而不是 localhost

To fix my issue, I have 3 ideas:

  1. I might hardcode the IP address of each server into a PHP variable in a config file that we have on each server.
  2. I might use the reverse proxy add forward module for Apache (mod_rpaf).
  3. I might change the proxy_pass on each server to go to XXX.XXX.XXX.XXX:8880 and YYY.YYY.YYY.YYY:8880 rather than localhost?
无法言说的痛 2024-09-16 06:40:58

你真的应该有一些服务器特定的配置被加载,里面有一个服务器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?)

痴骨ら 2024-09-16 06:40:58

在 Apache 2.4 及更高版本中 $_SERVER['SERVER_ADDR'] 如果您从本地主机调用服务器,它将返回类似 ::1 而不是 127.0.0.1

要解决这个问题,请转到您的 Apache conf 文件并更改

Listen 80

Listen 0.0.0.0:80

最后确保您的服务器不在反向代理后面

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

Listen 80

to

Listen 0.0.0.0:80

Finally make sure that your server is not behind a reverse proxy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文