Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Haproxy to enable running a websocket server and Lighttpd web server on the same machine. I want to retrieve the machines external IP address from the web application running on the Lighttpd server.

Without haproxy this works:


    $myip = $_SERVER['SERVER_ADDR'];
    $myurl = 'http://'.$myip.'/pathToProject/';
    define('URL', $myurl);

But behind the proxy $_SERVER['SERVER_ADDR']; returns which does not work with the PHP framework I am using.


    maxconn     4096
    nbproc      1

    mode        http

frontend all
    timeout client 86400000
    default_backend www_backend
    acl is_websocket hdr(Upgrade) -i WebSocket
    acl is_websocket hdr_beg(Host) -i ws
    use_backend socket_backend if is_websocket

backend www_backend
    balance roundrobin
    option forwardfor
    timeout server 30000
    timeout connect 4000
    server apiserver weight 1 maxconn 1024 check

backend socket_backend
    balance roundrobin
    option forwardfor
    timeout queue 5000
    timeout server 86400000
    timeout connect 86400000
    server apiserver weight 1 maxconn 1024 check
share|improve this question

You will need to add the option "option forwardfor" in your HAproxy config file, so that HAproxy will add a new header with the visitor real IP address.

After that, in your PHP code, check for the HTTP_X_FORWARDED_FOR header, instead of REMOTE_ADDR

foreach (getallheaders() as $name => $value) {
    echo "$name: $value<br>\n";

A full detailed answer can be found here: haproxy and forwarding client IP address to servers

share|improve this answer
My server machine does not have a static IP, its an embedded device that will get a new IP in each network. The PHP application needs to know what IP address the client connects to - HOST IP, not the clients IP. – Oskar Cronwall May 15 '14 at 13:05
@OskarCronwall You want the IP of which device, exactly? – Michael Hampton May 15 '14 at 13:15
Embedded linux device (my machine) is running Haproxy, Lighttpd and Websocket server. PHP application is running on my Lighttpd server. From the PHP application I want to find out the IP of the embedded linux device. Not the clients IP that connects to the device. – Oskar Cronwall May 15 '14 at 19:50

In theory, this is exactly what you can solve using

    option originalto

in your frontend, as per the docs.

Your application will have to rely on the X-Original-To header.

share|improve this answer
My question was how do I access host IP in PHP given these circumstances. Is there a way to configure Lighttpd to access this header (X-Original-To) through PHP? Google gives me nothing. – Oskar Cronwall May 19 '14 at 13:09
Google gave me this:…. In short, you can use the $_SERVER associative array, or the apache_request_headers() function, apparently. – Felix Frank May 19 '14 at 13:15

For Apache you need something like mod rpaf. I don't know this for Lighttpd, but maybe the links here can help you;

share|improve this answer
This gives me the clients IP right? How do I use it to retrieve my IP? – Oskar Cronwall May 15 '14 at 11:20
Depends on the mod_rpaf configuration, I think without any config you have to use x-forwarded-for, but you can configure quite alot (for example logfile markup). – Jeroen May 15 '14 at 12:27
@Jeroen no, Oskar is right - mod_rpaf does concern itself only with the client's IP via X-Forwarded-For and friends. This won't cut it. – Felix Frank May 16 '14 at 15:26
up vote -2 down vote accepted

This is how I solved it (only works on linux):

$myip = shell_exec("ifconfig eth0 | awk '/inet / {print$2}' | cut -d: -f2");
$myurl = 'http://'.$myip.'/page/';
define('URL', $myurl);
share|improve this answer
This can only work if your stack is so simple that PHP runs on the same machine as haproxy. It goes further and assumes that the relevant IP is bound to eth0, and that it is its primary address. In all, this is far from a general solution. – Felix Frank May 19 '14 at 12:41
That is correct, my applications run on a single machine and IP is bound to eth0. This answer is however a working method for retrieving host IP, whereas every other answer are methods for retrieving client or remote IP, which is something totally different. That is why I posted this method. – Oskar Cronwall May 19 '14 at 13:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.