The PHP variable $ _SERVER ['SERVER_ADDR] always returns 127.0.0.1

We have several load-loaded web servers running the same PHP webapp ( LAMP ), and I would like to run slightly different code on each server (for testing purposes). I was hoping to use super-global $_SERVER['SERVER_ADDR']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 does not work because both machines are set $_SERVER['SERVER_ADDR']to 127.0.0.1 . How do I configure them to $_SERVER['SERVER_ADDR']be set to their public IP address?

I assume that the problem may be related /etc/hosts, so for reference, it looks like this:

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

Updating ...

,! nginx -. - nginx - nginx conf:

location / {
    root                  /var/www/staging/current;
    proxy_pass            http://localhost:8880;
}
+3
6

, ,

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

, . , .

, . :

$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);
}
+4

/etc/hosts

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

+2

, 3 :

  • IP- PHP , .
  • - Apache (mod_rpaf).
  • proxy_pass , XXX.XXX.XXX.XXX:8880 YYY.YYY.YYY.YYY:8880, localhost?
0

som, , . - ip-, , , . , , (, some1 geths , ? )

0

!

echo getHostByName($_SERVER[HTTP_HOST]);
0

Source: https://habr.com/ru/post/1753690/


All Articles