Skip to content

How to Configure Nginx Real IP Module

DodaTech Updated 2026-06-24 1 min read

The real_ip module restores the original client IP when Nginx is behind proxies or load balancers. Proper proxy trust configuration is essential. This guide walks through the specific troubleshooting steps to diagnose and resolve real IP issues.

Before You Begin

Before you begin, be sure to have the following in place:

  • A Linux server with the relevant software installed
  • Access to the command line interface
  • Appropriate permissions (root or sudo)

Quick Fix

Wrong

Without real_ip configuration (logs show proxy IPs)

Wrong: Original client IP lost behind proxies

set_real_ip_from 10.0.0.0/8;\nset_real_ip_from 172.16.0.0/12;\nreal_ip_header X-Forwarded-For;\nreal_ip_recursive on;

Right: Using real_ip module to restore true client IP

Output

Real IP module configured:\n  Trusted proxies: 10.0.0.0/8, 172.16.0.0/12\n  Header: X-Forwarded-For\n  Recursive: on

Prevention

To avoid future issues, follow these best practices:

  • Define trusted proxy subnets with set_real_ip_from
  • Use X-Forwarded-For as the real IP header
  • Set real_ip_recursive on for multi-proxy setups
  • Trust only specific proxy IPs, not all X-Forwarded-For values
  • Test with logging: log_format with $remote_addr

DodaTech Tools

For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.

Common Mistakes with real ip

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world NGINX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Why does my Nginx access log show proxy IPs instead of real client IPs?|||Without real_ip configuration, $remote_addr contains the IP of the last proxy. Configure set_real_ip_from to trust proxy IPs and use X-Forwarded-For.
What does real_ip_recursive on do? With multiple proxies, recursive on replaces $remote_addr with the last untrusted IP in the X-Forwarded-For chain.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro