What is a Reverse Proxy
A reverse proxy sits between clients and servers, forwarding requests to backend servers.
Forward vs Reverse Proxy
Forward Proxy (client-side):
Client → Proxy → Internet
(Proxy hides client identity)
Reverse Proxy (server-side):
Client → Proxy → Server
(Proxy hides server identity)
How Reverse Proxy Works
┌─────────────────┐
│ Reverse Proxy │
│ (Nginx) │
Client ────→ ─────→│ │
│ ┌───────────┐ │
│ │ Request │ │
│ │ Router │ │
│ └─────┬─────┘ │
└────────┼─────────┘
┌─────────────┼─────────────┐
│ │ │
┌──────▼──┐ ┌──────▼──┐ ┌──────▼──┐
│Server 1 │ │Server 2 │ │Server 3 │
└─────────┘ └─────────┘ └─────────┘
Reverse Proxy Functions
| Function | Description |
|---|---|
| Load Balancing | Distribute requests across servers |
| SSL Termination | Handle HTTPS encryption/decryption |
| Caching | Cache static content |
| Compression | Compress responses |
| Rate Limiting | Limit request rates |
| Security | Hide backend servers |
| Logging | Centralized request logging |
Client Perspective
Client sees:
- Single endpoint (proxy address)
- HTTPS (handled by proxy)
- Fast responses (cached)
Client doesn't see:
- Backend server architecture
- Number of servers
- Server software
- Internal network
Nginx
Nginx is the most popular reverse proxy and web server.
Nginx Configuration
# Basic reverse proxy
upstream backend {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Nginx Features
1. Load Balancing
upstream backend {
least_conn; # Algorithm
server 10.0.0.1:3000;
server 10.0.0.2:3000;
}
2. SSL Termination
server {
listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
# Backend gets HTTP
}
3. Caching
proxy_cache_path /tmp/cache levels=1:2;
location /static/ {
proxy_cache my_cache;
proxy_cache_valid 200 1h;
}
4. Rate Limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
location /api/ {
limit_req zone=api burst=20;
}
Nginx vs Apache
| Aspect | Nginx | Apache |
|---|---|---|
| Architecture | Event-driven | Process/thread-based |
| Performance | Higher concurrency | Lower concurrency |
| Memory | Lower usage | Higher usage |
| Config | Declarative | .htaccess |
| Use Case | Reverse proxy, static | Dynamic content |
Common Nginx Patterns
1. Static file serving
location /static/ {
root /var/www;
expires 30d;
}
2. API proxy
location /api/ {
proxy_pass http://api_backend;
}
3. WebSocket proxy
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
}
Benefits and Use Cases
Reverse proxies provide significant benefits for production systems.
Security Benefits
1. Hide Backend Architecture
Client → Proxy → Server1, Server2, Server3
Client can't see server IPs or count
2. SSL Termination
Client ←──HTTPS──→ Proxy ←──HTTP──→ Server
Servers don't handle SSL overhead
3. DDoS Protection
- Rate limiting
- IP blocking
- Request validation
4. WAF (Web Application Firewall)
- SQL injection prevention
- XSS prevention
- Bot detection
Performance Benefits
1. Caching
- Static files cached at proxy
- Reduces backend load
2. Compression
- gzip/brotli compression
- Reduces bandwidth
3. Connection Pooling
- Reuse backend connections
- Reduce overhead
4. SSL Offloading
- Dedicated SSL processing
- Better performance
Operational Benefits
1. Centralized Logging
All requests logged at proxy level
2. Easy Backend Changes
Add/remove servers without client impact
3. Zero-Downtime Deployment
- Rolling updates
- Blue-green deployment
4. Health Checks
Monitor backend health
Remove unhealthy servers
Use Cases
| Use Case | How Proxy Helps |
|---|---|
| Web application | Load balancing, SSL, caching |
| Microservices | API gateway, routing |
| API management | Rate limiting, authentication |
| Static site | CDN, caching |
| Legacy modernization | New frontend, old backend |
Architecture with Reverse Proxy
┌─────────────┐
│ Nginx │
│ (Proxy) │
Client ────HTTPS───→│ │
│ SSL Term │
│ Cache │
│ Rate Limit │
└──────┬──────┘
│
┌────────────┼────────────┐
│ │ │
┌──────▼──┐ ┌──────▼──┐ ┌──────▼──┐
│Web App 1│ │Web App 2│ │API Server│
└─────────┘ └─────────┘ └──────────┘
Practice Problems
Design a scalable Reverse Proxy system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Reverse Proxy to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Reverse Proxy and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What is the main difference between a forward proxy and a reverse proxy?
2. What is SSL termination?
3. Why is Nginx popular as a reverse proxy?
4. How does a reverse proxy improve security?
Flashcards
Question
What is a reverse proxy?
Click to reveal answer
Answer
A server that sits between clients and backend servers, forwarding requests. It hides backend architecture, provides load balancing, SSL termination, caching, and security.
Question
What is SSL termination?
Click to reveal answer
Answer
Handling SSL encryption/decryption at the proxy level, so backend servers receive plain HTTP. Reduces backend overhead and simplifies certificate management.
Question
What are the main benefits of a reverse proxy?
Click to reveal answer
Answer
Security (hide backends), Performance (caching, compression), Operations (centralized logging, health checks), Load balancing, SSL termination.
Question
What is Nginx?
Click to reveal answer
Answer
A popular web server and reverse proxy with event-driven architecture. Features: load balancing, SSL termination, caching, rate limiting, and high performance.
Question
What is Reverse Proxy?
Click to reveal answer
Answer
Reverse Proxy is a key concept in system design.
Revision Notes
Key Takeaways
- 1.Reverse proxy hides backend architecture from clients
- 2.SSL termination reduces backend overhead
- 3.Nginx is the most popular reverse proxy with event-driven architecture
- 4.Reverse proxies enable load balancing, caching, and rate limiting
- 5.Essential for production systems requiring security and performance
Interview Tips
- •Always include a reverse proxy in your system design
- •Discuss SSL termination and its benefits
- •Mention load balancing as a key function
- •Consider caching at the proxy level for static assets
Cheat Sheet
Reverse Proxy - Cheat Sheet
What it does:
Sits between clients and servers, forwarding requests.
Key Functions:
- Load balancing
- SSL termination
- Caching
- Compression
- Rate limiting
- Security (hide backends)
- Logging
Nginx Config Highlights:
- upstream: Define backend servers
- proxy_pass: Forward requests
- ssl_certificate: SSL termination
- proxy_cache: Caching
- limit_req: Rate limiting
Benefits:
Security: Hide architecture, SSL offloading
Performance: Caching, compression
Operations: Health checks, zero-downtime deploy