0

I'm running an apache web server behind an apache reverse proxy. The problem that I have is that if I put in the backend server a php file containing <?php echo '<pre>Server IP: '; print_r($_SERVER['SERVER_ADDR']); echo '</pre>'; ?> the answer I get is the internal ip (192.168.1.20) and not the external ip of the proxy. Any hints?

  • check out these questions: https://stackoverflow.com/questions/3202872/php-server-server-addr-variable-always-returns-127-0-0-1 and https://serverfault.com/questions/844529/nslookup-returns-different-ip-than-serverserver-addr – Martin Mar 08 '21 at 13:19
  • I have already checked them. Actually the second one describes exactly my problem. The site is behind a proxy and the php function returns a different ip from the nslookup command. I'm looking for a way to change the response of $_SERVER['SERVER_ADDR'] to the proxies external ip. – Vagelis Melidonis Mar 08 '21 at 13:25
  • I doubt that changing the SERVER_ADDR variable is possible, you should consider other ways... for example on your reverse proxy, but the IP inside the X-Forwarded-For HTTP-Header field, and read that header inside your PHP script... Or you use one of the solutions pointed out in the other questions – Martin Mar 08 '21 at 13:48
  • The problem is that I can't change anything in the script. It runs remotely and has to match the server_addr with the external ip of the proxy. – Vagelis Melidonis Mar 08 '21 at 17:49
  • You have no way to know this information unless that proxy explicitly provides it to you. – Michael Hampton Mar 08 '21 at 19:10

1 Answers1

0

$_SERVER['SERVER_ADDR'] is a field set by the php interpreter itself and is set to:

The IP address of the server under which the current script is executing.

Source: Link

So, your php script assumes the public IP in that variable (which is wrong - that script needs to be modified, because many webservers do not listen to a public IP address - and that field contains the ip address the webserver is bound to, which can be a private address! ), and you cannot change the source code of the script.

In this case you have two options:

  • remove the reverse proxy so that your main webserver is bound to the public IP address
  • change the source code of the php interpreter in order to have a setting inside the php.ini file to "set" this variable to a string setting.

But unless you are a software developper and you really know what you are doing, I really really would advise against option two.

Sorry, but there simply are no other options, because you outruled any other option available...

Martin
  • 2,194
  • 7
  • 16