Reverse Proxy: What It Is and How to Set It Up

Reverse proxy protects your server and distributes load. Nginx or Caddy. We explain the concept and setup — with Node.js and Laravel examples.
What Is a Reverse Proxy?
A reverse proxy is an intermediary server that sits between clients and your application servers. Instead of clients connecting directly to your application, requests pass through the reverse proxy first, which routes them to the correct destination.
The distinction from a forward proxy is straightforward: a forward proxy protects clients and hides their identity, while a reverse proxy protects servers and hides your infrastructure.
Why You Need a Reverse Proxy
1. Security
A reverse proxy hides the real IP address of your application server. Attackers only see the proxy address, making it significantly harder to target your actual server. You can also apply WAF rules and filter requests before they reach your application.
2. Load Balancing
Running multiple servers for the same application? A reverse proxy distributes requests across them evenly or using specific algorithms like Round Robin, Least Connections, or IP Hash.
3. Caching
The reverse proxy stores copies of frequently requested responses and serves them directly without hitting the application server. This dramatically reduces backend load and improves response times.
4. SSL Termination
Instead of every application server handling SSL certificates, the reverse proxy manages encryption centrally. Communication between the proxy and application servers uses plain HTTP within your private network.
Available Options
| Software | Best For | Performance | Setup Difficulty |
|---|---|---|---|
| Nginx | High-traffic sites | Excellent | Moderate |
| Apache | Traditional PHP environments | Good | Easy |
| HAProxy | Advanced load balancing | Excellent | Advanced |
| Caddy | Small to medium projects | Good | Very easy |
| Traefik | Docker and Kubernetes | Good | Moderate |
Setting Up Nginx as a Reverse Proxy
Step 1: Install Nginx
On Ubuntu/Debian:
sudo apt update
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
Step 2: Create Site Configuration
Create a new configuration file:
sudo nano /etc/nginx/sites-available/myapp
Basic configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Step 3: Enable the Site
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Step 4: Add SSL with Let's Encrypt
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com
Certbot automatically modifies Nginx configuration and installs a free SSL certificate with automatic renewal.
Advanced Configuration
Caching
proxy_cache_path /var/cache/nginx levels=1:2
keys_zone=app_cache:10m max_size=1g inactive=60m;
server {
location / {
proxy_cache app_cache;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 1m;
proxy_pass http://127.0.0.1:3000;
}
}
Load Balancing
upstream backend {
least_conn;
server 10.0.0.1:3000 weight=3;
server 10.0.0.2:3000 weight=2;
server 10.0.0.3:3000 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
DDoS Protection
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://127.0.0.1:3000;
}
}
Reverse Proxy for Multiple Applications
A common scenario: one server running multiple applications on different ports:
server {
server_name app1.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
server {
server_name app2.example.com;
location / {
proxy_pass http://127.0.0.1:4000;
}
}
server {
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
Each application runs on a different port, and Nginx routes requests based on the subdomain.
Performance Monitoring
After setup, monitor performance:
- Access logs: Check
/var/log/nginx/access.logfor request patterns - Error logs: Monitor
/var/log/nginx/error.logfor issues - Connection status: Enable
stub_statusto monitor active connections - Response time: Use the
$upstream_response_timevariable in access logs
Common Errors and Solutions
| Error | Cause | Solution |
|---|---|---|
| 502 Bad Gateway | Application server is down | Verify the app is running on the correct port |
| 504 Gateway Timeout | Application is slow | Increase proxy_read_timeout value |
| WebSocket not working | Missing Upgrade headers | Add proxy_set_header Upgrade and Connection |
| Real IP lost | Missing headers | Add X-Real-IP and X-Forwarded-For |
Conclusion
A reverse proxy is an essential component of any professional infrastructure. It adds security, performance, and control between users and your application. On CLOUD (cloud.net.sa) servers, you get full root access to configure Nginx or any reverse proxy you prefer, with consistent performance and very low latency from Riyadh. Start with the basic setup and add advanced features as your needs grow.
Get Started
Ready to build your infrastructure? Configure your cloud server to your exact specs, or view hosting plans.


