1

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:

PHP

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

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

haproxy.conf

global
    maxconn     4096
    nbproc      1

defaults
    mode        http

frontend all 0.0.0.0:80
    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 127.0.0.1:8080 weight 1 maxconn 1024 check

backend socket_backend
    balance roundrobin
    option forwardfor
    timeout queue 5000
    timeout server 86400000
    timeout connect 86400000
    server apiserver 127.0.0.1:8082 weight 1 maxconn 1024 check

5 Answers5

2

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

MTIhai
  • 206
  • 1
  • 3
  • 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
1

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.

Felix Frank
  • 3,063
  • 1
  • 15
  • 22
  • 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: http://stackoverflow.com/questions/541430/how-do-i-read-any-request-header-in-php. In short, you can use the `$_SERVER` associative array, or the `apache_request_headers()` function, apparently. – Felix Frank May 19 '14 at 13:15
1

thanks guys it works....

1) edit file nano /etc/haproxy/haproxy.cfg and add there

    defaults
      mode http
      option forwardfor 

2) and in php

      <?php 
      echo '<pre>'; print_r(getallheaders()); echo '</pre>';


      // PROBLEM IS each browser has different "x-forwarder-for" some "X-Forwarder-for" and some browser gave you 2x ip "x.x.x.x, x.x.x.x"
      foreach ( getallheaders() as $k => $v) {
        if ( strtolower($k) == 'x-forwarded-for' ) {
          $tmp = explode(', ', $v);
          if ( $tmp[count($tmp)-1] != '' )
             $_SERVER['REMOTE_ADDR'] = $tmp[count($tmp)-1];     
        }
      }


      echo $_SERVER['REMOTE_ADDR'];
      ?>

3) you can see IP address

0

For Apache you need something like mod rpaf. I don't know this for Lighttpd, but maybe the links here can help you; http://lowendtalk.com/discussion/2259/real-ip-module-for-lighttpd

Jeroen
  • 1,339
  • 7
  • 16
  • 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
-2

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