[Solved] Varnish WordPress comments IP address problem

First, let me explain the Varnish WordPress comments IP address problem. As many of you may know I have switched to VPS from Digital Ocean and currently run a LEMP stack with Varnish Cache. All was well until recently when I discovered that all WordPress comments IP address were 127.0.0.1. This obviously cannot be right. I went back and looked at my comments from the past and the WordPress comments IP addresses were correct. Researching around I found out that Varnish Cache was the culprit. Varnish is a caching mechanism that supercharges serving static content. Let us see how to fix Varnish WordPress comments IP address problem. [Read: Setup a self-hosted CDN to speedup WordPress]

Varnish Wordpress Comments Ip Address Problem
Varnish Wordpress Comments Ip Address Problem

Varnish WordPress Comments IP Address Problem Solution

While there are multiple ways to fix this, I am going to show you the way to fix Varnish Cache WordPress Comments IP address issue universally. This means the solution will apply to all the websites that you run on the same server. Needless to say this will also work even if you have only one website running on the server. [Read: GoDaddy vs Synthesis vs Digital Ocean speed comparison]

Step 1

First, add create a file called realip.php in a location of your choice (I recommend /usr/share/php/).

sudo nano /usr/share/php/realip.php

Copy the following contents to the file:

< ?php
if ( isset( $_SERVER[ "HTTP_X_FORWARDED_FOR" ] ) ) {
  $_SERVER[ 'REMOTE_ADDR' ] = $_SERVER[ "HTTP_X_FORWARDED_FOR" ];
}
?>

Save and exit. Now you have to call this script into PHP to solve the Varnish WordPress comments IP address problem. [Read: 5 blogging mistakes I made when I started this blog]

Recommended Guides on WordPress:

Step 2

Now open your php.ini file. Typically, this is located under /etc/php5. On Nginx with PHP-FPM, it under /etc/php5/fpm/. Find the following code section:

; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file =

Change it as shown below to call the realip.php file you created above:

; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file = /usr/share/php/realip.php

If you are using WordPress you may skip both Step 1 and Step 2, and add the PHP code from Step 1 directly into your wp-config.php. But keep in mind that this will only take effect on the WordPress site you are adding this code to and not all of your sites (including other WordPress sites on the same server). Save, exit, and restart your webserver.

Step 3

On Nginx you do this by issuing the following command:

sudo nginx -t && sudo service nginx reload

On Apache, use:

sudo service apache2 reload

This should have fixed your Varnish WordPress comments IP address problem. You should not see comments IP address 127.0.0.1 anymore. You can test by submitting a test command to verify. [Read: Digital Ocean vs Synthesis – 10 reasons why I chose Digital Ocean VPS]

Wordpress Comments Ip Address - Before And After
Wordpress Comments Ip Address - Before And After

So there you go. Whether you are using Varnish cache with Apache or Nginx, the above solution should fix the Varnish WordPress comments IP address problem.

Be the 1 in 200,000. Help us sustain what we do.
113 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.

Try Deployarr