Proxy Security¶
When your application is behind a proxy, load balancer, or CDN, properly handling the X-Forwarded-For header is critical for security.
Secure Configuration¶
GUARD_SECURITY_CONFIG = SecurityConfig(
trusted_proxies=["10.0.0.1", "192.168.1.0/24"],
trusted_proxy_depth=1,
trust_x_forwarded_proto=True,
)
How It Works¶
- When a request arrives, DjangoAPI Guard checks if it's from a trusted proxy
- If not from a trusted proxy, the direct connecting IP is always used
- If from a trusted proxy, the X-Forwarded-For header is parsed
- The extracted IP is then used for all security checks
Real-World Examples¶
Single Reverse Proxy:
GUARD_SECURITY_CONFIG = SecurityConfig(
trusted_proxies=["10.0.0.1"],
trusted_proxy_depth=1,
trust_x_forwarded_proto=True
)
Load Balancer + Proxy:
GUARD_SECURITY_CONFIG = SecurityConfig(
trusted_proxies=["10.0.0.1", "192.168.1.0/24"],
trusted_proxy_depth=2,
trust_x_forwarded_proto=True
)
Best Practices¶
- Be specific: Only include the exact IPs or ranges of your known proxies
- Use correct depth: Configure based on your actual proxy chain
- Regular audits: Periodically review your trusted proxy list
- Test configuration: Verify correct IP extraction in your environment