PHP $_SERVER[‘SERVER_ADDR’] variable always returns 127.0.0.1

谁说我不能喝 提交于 2020-01-03 19:57:14

问题


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

回答1:


Surely it's as simple as

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



回答2:


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



回答3:


reverse rows in /etc/hosts

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

should work




回答4:


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?



回答5:


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?)




回答6:


Use this more accurate!

echo getHostByName($_SERVER[HTTP_HOST]);


来源:https://stackoverflow.com/questions/3202872/php-server-server-addr-variable-always-returns-127-0-0-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!