Fix – WordPress Showing Localhost IP(127.0.0.1) for Comment Author Instead of Real


So, you are using Varnish? Cool! That's why you are unable to see real IP of your visitors. No worries, here's the complete solution.

To fix this problem,

  1. Login to FTP/SFTP
  2. Go to public_html folder
  3. Edit wp-config.php
  4. Add below code just before /* That's all, stop editing! Happy blogging. */
  5. Save Changes.
/** Required Code for showing correct IP address */
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
$xffaddrs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']); 
$_SERVER['REMOTE_ADDR'] = $xffaddrs[0]; 
}

Mark: This little problem I had recently faced on Cloudways web hosting. However, it is very optimized for better performance. I love it.

Alternative case

Similarly, if your IP is showing incorrect after using Cloudflare, here's the fix –

/** Correct IP in case of Cloudflare **/
if(array_key_exists('HTTP_CF_CONNECTING_IP', $_SERVER)){ 
 $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; 
}

Alternatively, if you have LEMP stack with root access, then add below rule under Server Block. You can find your configuration file at /etc/nginx/sites-available/ directory.

server {
root /var/www/html;
index index.php index.html index.htm;
server_name .example.com;
# Get real IP
set_real_ip_from 0.0.0.0/0;
set_real_ip_from ::/0;
real_ip_header X-Forwarded-For;
...

In case, if you don't feel comfort in editing wp-config.php file, add above snippet using Code Snippets plugin. It will work fine. I hope this guide helps in the right direction. Thanks

18 thoughts on “Fix – WordPress Showing Localhost IP(127.0.0.1) for Comment Author Instead of Real”

    • Hi,
      Thanks for coming back.

      You need to put code just before this line
      /* That's all, stop editing! Happy blogging. */

      Reply
  1. Hi, I've found your blog from shoutmeloud forum, and it's great for optimizing wordpress.

    What plugin/code you're using for the responsive mobile menu at the left corner of the header? I need one badly.

    Reply
  2. I was looking for the same. It is showing 127.0.0.1. Fixed it. your blog seems very handy for wordpress fixes/optimizations. thanks.

    Reply
  3. That's the exact problem I was facing. The Cloudways support did rectify it twice but they never shared the solution. It was Mustasaam who referred me to this page.

    The solution works perfectly for me. Thank you for sharing.

    Just one more query, how did you hide the Website option from the comments box? Most of my readers are internet noobs who feel filling out the Website section is mandatory. I too want to get the same removed. Mind sharing an article on my email ID?

    Reply
    • Thanks for visiting.

      To hide website field, you can use below snippet.

      // Remove URL Field
      function remove_url_comments($fields) {
      unset($fields['url']);
      return $fields;
      }
      add_filter('comment_form_default_fields','remove_url_comments');
      Reply

Leave a Comment