If your WordPress website or blog is behind a proxy or load balancer server, chances are, you’re getting only one IP address or subsets of IP addresses from the proxy servers or load balancers. That’s because they receive and forward HTTP requests from and to the web clients which means your WordPress site only sees the IP addresses from the proxy servers.

If you’re using services like Google Pagespeed Service or Cloudflare then you will know what I am talking about. Using these proxy servers to help speed up your website is great, but they also create an issue where WordPress sees only the proxy server’s IP addresses and not the real clients.

Here are some of the reasons you may want the real web client IP addresses;  set restrictions based on IP address, identify spam IP addresses, use plugins that block spams by IP addresses, like Akismet. Without setting the correct X-Forwarded-For in WordPress, you won’t be able to use some of these plugins.

This brief tutorial is going to show you how to fix that in WordPress so you can use plugins that identify the real IPs of the clients.

Before you continue, check your comments folder in WordPress and see if all the IP addresses are the same. If they are, then continue below. If not, then you may not have a problem with real IP.

To get started, browse and open wp-config.php file in the root directory and add these lines below at the top and save it.

if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$xffaddrs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $xffaddrs[0];
 }

 

wordpress_real-ip

 

That’s it. Now wait until new comments come in and check to see if they are coming from the same IP addresses..

 

Enjoy!