- Updated ProxyFix configuration for Cloudflare + nginx - Added custom IP detection for real client IPs - Updated rate limiting to use real IPs - Added nginx configuration example - Added Cloudflare setup guide - Added production server setup script
4.0 KiB
4.0 KiB
Cloudflare + nginx Setup Guide for ircquotes
Overview
This setup ensures that your ircquotes application can see real client IP addresses even when behind:
- Cloudflare (CDN/Proxy)
- nginx (Reverse Proxy)
- Gunicorn (WSGI Server)
Architecture
Client → Cloudflare → nginx → Gunicorn → ircquotes
Setup Steps
1. Cloudflare Configuration
Enable Proxy (Orange Cloud)
- Set your DNS record to "Proxied" (orange cloud icon)
- This routes traffic through Cloudflare's edge servers
Recommended Cloudflare Settings:
- SSL/TLS: Full (Strict) if you have SSL on origin
- Security Level: Medium
- Bot Fight Mode: Enabled
- Rate Limiting: Configure as needed
- Page Rules: Optional caching rules
Important Headers:
Cloudflare automatically adds these headers:
CF-Connecting-IP: Real client IP addressCF-Ray: Request identifierCF-Visitor: Visitor information
2. nginx Configuration
Copy the provided nginx-ircquotes.conf to your nginx sites:
sudo cp nginx-ircquotes.conf /etc/nginx/sites-available/ircquotes
sudo ln -s /etc/nginx/sites-available/ircquotes /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Key nginx features:
- ✅ Cloudflare IP range restoration
- ✅ Real IP detection via CF-Connecting-IP
- ✅ Additional rate limiting layer
- ✅ Security headers
- ✅ Gzip compression
- ✅ Static file optimization
3. Application Configuration
The ircquotes app is already configured to:
- ✅ Use
CF-Connecting-IPheader (Cloudflare's real IP) - ✅ Fall back to
X-Forwarded-ForandX-Real-IP - ✅ Handle 2-proxy setup (Cloudflare + nginx)
- ✅ Rate limit by real client IP
4. Verification
To verify real IPs are being detected:
-
Check application logs:
tail -f /var/log/ircquotes/access.log -
Test from different locations:
- Visit your site from different networks
- Check admin panel for real IPs in quote submissions
- Verify rate limiting works per real IP
-
Debug headers (temporary debug route):
@app.route('/debug-headers') def debug_headers(): return jsonify({ 'real_ip': get_real_ip(), 'cf_connecting_ip': request.headers.get('CF-Connecting-IP'), 'x_forwarded_for': request.headers.get('X-Forwarded-For'), 'x_real_ip': request.headers.get('X-Real-IP'), 'remote_addr': request.remote_addr })
5. Security Considerations
Cloudflare Settings:
- Enable DDoS Protection
- Configure WAF Rules for your application
- Set up Rate Limiting at Cloudflare level
- Enable Bot Management if available
nginx Security:
- Keep Cloudflare IP ranges updated
- Monitor for suspicious patterns
- Implement additional rate limiting
- Regular security updates
Application Security:
- All security features already implemented
- Rate limiting per real IP
- CSRF protection enabled
- Input validation active
Troubleshooting
IPs showing as 127.0.0.1:
- Check nginx is passing headers correctly
- Verify Cloudflare IP ranges in nginx config
- Ensure ProxyFix is configured for 2 proxies
- Check
CF-Connecting-IPheader presence
Rate limiting not working:
- Verify real IP detection is working
- Check rate limiting configuration
- Monitor nginx and application logs
- Test with different source IPs
Performance issues:
- Enable nginx caching for static files
- Configure Cloudflare caching rules
- Monitor Gunicorn worker count
- Check database connection pooling
Monitoring
Recommended monitoring:
- Application logs: Real IP addresses in logs
- nginx access logs: Request patterns
- Cloudflare Analytics: Traffic patterns
- Rate limiting metrics: Blocked vs allowed requests
Production Checklist
- Cloudflare proxy enabled (orange cloud)
- nginx configuration deployed
- Real IP detection working
- Rate limiting functional
- Security headers present
- SSL/TLS configured
- Monitoring in place
- Backup and recovery tested