Menu Close

How to pass visitors real IP to WordPress from Nginx reverse proxy?

Pass real ip to wordpress

In this tutorial, I will teach you how to pass the real IP to WordPress correctly.

One of many experienced problems with Nginx was with passing the real IP of visitors to a service behind it, like WordPress.

It may seem like a rookie problem, but sometimes it happens!

When you have a WordPress website running behind a reverse proxy, you must keep the user information integrity all the way to the final destination.

If you don’t set some basic headers, you’ll experience the IP of reverse proxy (Nginx) in your WordPress logs or comments. Also, you’ll comment will be marked as spam, so watch out for this problem.

The first time, I got this problem when I was running a WordPress container in the same network with an Nginx container. It ended up with Nginx container’s IP in my WordPress logs and analytics. So I have searched like many of you, for this problem on Google.

How to solve the issue with the real IP to WordPress

Open your custom Nginx configuration and in the location block from server one, put the lines below.
If you don’t know where a configuration is, try in etc/nginx/sites-available/... path.

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

These will add 2 new headers X-Real-IP and X-Forwarded-For to transfer the initial information from the visitor to our proxied WordPress.

Example

server {
        server_name yourwebsite.com;
                location / {
                        proxy_redirect off;
                        proxy_set_header Host $host;
                        # lines to pass the real ip
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://wordpress:80;
        }
}

This example of IP passing can be applied to other services too.
Also, if your WordPress still doesn’t recognize the passed header, you must add the following code in wp-config.php from your WordPress installation:

// Code for showing correct client IP address
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { 
    $mte_xffaddrs = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ); 
    $_SERVER['REMOTE_ADDR'] = $mte_xffaddrs[0]; 
}

The above snippet takes all passed IP addresses from HTTP_X_FORWARDED_FOR and pass them to the REMOTE_ADDR.

Useful tip

If you don’t want to mess with the original configuration of WordPress to add the above code, you can use a plugin that is doing the same.

Proxy Real IP looks for following HTTP headers: X-FORWARDED-FOR, X-FORWARDED, FORWARDED-FOR, FORWARDED, X-REAL-IP. You just have to install this plugin and enable it.

If you want to launch a quick WordPress container with Docker, check this tutorial How to run WordPress with docker on Linux/macOS/Windows?

My recommendation is to add the headers in your Nginx configuration and install the Proxy Real IP plugin. Good luck!

Spread the love

Leave a Reply