How to Configure Nginx Real IP Module
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
Right
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
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro