SoFunction
Updated on 2025-04-08

Implementation example of Nginx configuration real IP address forwarding

In many web applications, it is important to get the real IP address of the client, especially when using a reverse proxy server such as Nginx. This article will guide you how to configure it in NginxX-Real-IPandX-Forwarded-ForHead to ensure that your backend application can obtain the real client IP address.

1. Install Nginx

If Nginx is not installed, you can use the following command to install:

# For Ubuntu/Debian systemssudo apt update
sudo apt install nginx

# For CentOS/RHEL systemssudo yum install epel-release
sudo yum install nginx

2. Configure Nginx

Open Nginx configuration file, usually the path is/etc/nginx/or/etc/nginx/sites-available/default. Use the following command to open the file:

sudo nano /etc/nginx/

existserverAdd or modify the following content in the block:

server {
    listen 80;  # or 443, depending on your needs    server_name your_domain.com;  # Replace with your domain name or IP address
    location / {
        proxy_pass http://your_backend;  # Replace with your backend service address        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;  # Set up X-Real-IP        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # Set up X-Forwarded-For        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

3. Test configuration

After saving the configuration file, make sure that the configuration is free of errors. Test with the following command:

sudo nginx -t

If the output shows no errors, you can proceed to the next step.

4. Reload Nginx

After the test is correct, reload the Nginx configuration to make the changes take effect:

sudo systemctl reload nginx

5. Verify the configuration

In your backend application, use the following code to get the client's real IP address (assuming you are using Go):

func GetIP(r *) (string, error) {
    ip := ("X-Real-IP")
    if (ip) != nil {
        return ip, nil
    }

    ip = ("X-Forwarded-For")
    for _, i := range (ip, ",") {
        if (i) != nil {
            return i, nil
        }
    }

    ip, _, err := ()
    if err != nil {
        return "", err
    }

    if (ip) != nil {
        return ip, nil
    }

    return "", ("no valid ip found")
}

Summarize

Through the above steps, you have successfully configured Nginx to forward the client's real IP address to the backend application. This way, the backend application can correctly identify the visitor's real IP address. If you encounter any problems during the configuration process, you can view Nginx's error log for more information:

sudo tail -f /var/log/nginx/

This is the end of this article about the implementation example of Nginx configuration real IP address forwarding. For more related content on Nginx real IP address forwarding, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!