PHP $ _SERVER ['SERVER_ADDR']变量始终返回127.0.0.1 [英] PHP $_SERVER[‘SERVER_ADDR’] variable always returns 127.0.0.1

查看:1712
本文介绍了PHP $ _SERVER ['SERVER_ADDR']变量始终返回127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有多台负载均衡的Web服务器计算机运行同一PHP Web应用程序( LAMP ),而我想在每台服务器上运行略有不同的代码(出于测试目的 ).我希望使用$_SERVER['SERVER_ADDR'] super global来执行以下操作:

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";
}

不幸的是,这不起作用,因为两台计算机都将$_SERVER['SERVER_ADDR']设置为" 127.0.0.1 ".如何配置它们,以便将$_SERVER['SERVER_ADDR']设置为其公共IP地址?

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?

我想这个问题可能与/etc/hosts有关,因此出于参考目的,它目前看起来像这样:

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

更新...

糟糕!我忽略了在Web服务器之前考虑 nginx 反向代理.由于nginx conf中的以下行,所有到这些Web服务器的流量都来自nginx:

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;
}

推荐答案

当然很简单

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

这篇关于PHP $ _SERVER ['SERVER_ADDR']变量始终返回127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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