3b78cb3721646dd1c6241d669e98b1bf6d35eecb
By:
gestudio

Load Balancer forward client public IP address

April 11, 2017 388 views
Load Balancing

How can I access the public IP address of the client behind a DO load balancer?

Does the load balancer send any header with this IMPORTANT info? Can't find it in my $_SERVER info array :(

thanks!

5 Answers
jtittle1 April 11, 2017
Accepted Answer

@inigo3b78cb3721646dd1c6241

If you use var_dump( $_SERVER ); are you not seeing X-Forwarded-For?

You'll want to look for HTTP_X_ headers when it comes to fetching information coming from the LB, such as the proto (HTTP/HTTPS), port, and client IP.

HTTP_X_FORWARDED_PORT
HTTP_X_FORWARDED_PROTO
HTTP_X_FORWARDED_FOR

That's what I initially though about, but there is nothing in phpinfo() in the $server section.
Nothing like HTTP
XFORWARDED

weird

  • @inigo3b78cb3721646dd1c6241

    You shouldn't need to modify that specifically, unless you don't want to check for the standard headers and want to force REMOTE_ADDR to be the value of HTTP_X_FORWARDED_FOR.

    I tested this on a source compile of NGINX using the following fastcgi configuration:

    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 512 16k;
    fastcgi_busy_buffers_size 1m;
    fastcgi_temp_file_write_size 4m;
    fastcgi_max_temp_file_size 4m;
    fastcgi_intercept_errors off;
    
    fastcgi_param SCRIPT_FILENAME   $request_filename;
    fastcgi_param PATH_INFO         $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
    fastcgi_param QUERY_STRING      $query_string;
    fastcgi_param REQUEST_METHOD    $request_method;
    fastcgi_param CONTENT_TYPE      $content_type;
    fastcgi_param CONTENT_LENGTH    $content_length;
    fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
    fastcgi_param REQUEST_URI       $request_uri;
    fastcgi_param DOCUMENT_URI      $document_uri;
    fastcgi_param DOCUMENT_ROOT     $document_root;
    fastcgi_param SERVER_PROTOCOL   $server_protocol;
    fastcgi_param REQUEST_SCHEME    $scheme;
    fastcgi_param HTTPS             $https if_not_empty;
    fastcgi_param HTTP_PROXY        "";
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE   nginx/$nginx_version;
    fastcgi_param REMOTE_ADDR       $remote_addr;
    fastcgi_param REMOTE_PORT       $remote_port;
    fastcgi_param SERVER_ADDR       $server_addr;
    fastcgi_param SERVER_PORT       $server_port;
    fastcgi_param SERVER_NAME       $server_name;
    fastcgi_param REDIRECT_STATUS   200;
    

    The above is what would normally be in the fastcgi_params file.

Hey I got same issue too, this is my system
visitor -> load balancer -> my server[nginx -> apache]

How can i get the real remote ip address, please :(

I never figured it out. For a few minutes I thought the nginx fix posted in stackoverflow was the solution, but then I saw it was not working either, so I removed the DigitalOcean load balancers and setup one on my own with a cheaper $10 droplet.

Have another answer? Share your knowledge.