How to Pass a Visitor’s IP Address from Varnish to WordPress

Varnish caching can speed up the loading time of your website. Varnish listens on port 80 and passes data to the default HTTP application. This means that the HTTP log files will show your server’s IP instead of the actual remote IP of the visitor. This article will provide options for passing the remote IP to WordPress.

Get a Visitor’s IP Address for a WordPress Site

  1. Login to cPanel
  2. Open File Manager
  3. Open your wp-config.php file in the Code Editor
  4. Add the following code as a new line after the first line:
    if ( isset( $_SERVER[ "HTTP_X_FORWARDED_FOR" ])){ $_SERVER[ 'REMOTE_ADDR' ] = $_SERVER[ "HTTP_X_FORWARDED_FOR" ];}

Get a Visitor’s IP Address for All Sites by PHP Version

  1. Login to your server via SSH as root
  2. Create a PHP file at “/usr/local/php/remoteIP.php” with the following contents:
    <?php
        if ( isset( $_SERVER[ "HTTP_X_FORWARDED_FOR" ])){
            $_SERVER[ 'REMOTE_ADDR' ] = $_SERVER[ "HTTP_X_FORWARDED_FOR" ];
    }?>
  3. Login to WHM as root
  4. Open the MultiPHP INI Editor
  5. Add the full path for your new file at the end of the line that starts with “auto_prepend_file”
  6. Finally, click Save

Congratulations! You are now able to pass the correct IP address to WordPress from Varnish.

Was this article helpful? Let us know!