Guides

What Is a Reverse Proxy? Architecture, Benefits, and Setup

A reverse proxy sits between clients and your web server, handling requests, improving performance, and adding security. Here's how they work and how to set one up.

The Concept

A reverse proxy is a server that sits in front of your web servers, receiving client requests and forwarding them to the appropriate backend. It's "reverse" because a traditional (forward) proxy represents the client to the server; a reverse proxy represents the server to the client.

Think of it like a hotel reception desk. Guests (clients) talk to the receptionist (reverse proxy). The receptionist knows which room (backend server) handles which guest, handles common requests (luggage storage, key handover), and shields guests from the complexity of the hotel's internal layout.

How a Reverse Proxy Works

Client โ†’ Reverse Proxy โ†’ Backend Server(s)
         โ†‘               โ†‘
    (public IP)     (private IP, not directly accessible)

1. Client makes a request to yourdomain.com (which resolves to the reverse proxy's IP) 2. Reverse proxy receives the request 3. Reverse proxy evaluates the request against its configuration: - Is there a cached response? โ†’ Return it immediately - Which backend should handle this? โ†’ Route based on hostname, path, or other rules - Does this request need SSL/TLS? โ†’ Terminate SSL at the proxy, forward as HTTP internally - Is this a malicious request? โ†’ Apply rate limiting or WAF rules - Does the backend need load balancing? โ†’ Select least-loaded server 4. Reverse proxy forwards the request to the selected backend 5. Backend processes the request and returns a response 6. Reverse proxy optionally caches the response, then returns it to the client

Common Reverse Proxy Software

Nginx

The most popular reverse proxy. Fast, lightweight, well-documented.

# Basic Nginx reverse proxy configuration
server {
    listen 80;
    server_name yourdomain.com;

location / { proxy_pass http://127.0.0.1:3000; # Backend on port 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; } }

HAProxy

Purpose-built load balancer and reverse proxy. More powerful load balancing algorithms, detailed health checking, but more complex configuration than Nginx.

Traefik

Designed for container/microservice environments. Automatically discovers services through Docker, Kubernetes, or Consul. Configuration is dynamic โ€” new services are automatically routed without manual config changes.

Caddy

Automatic HTTPS with Let's Encrypt. Simple configuration syntax. Good for smaller deployments where simplicity matters.

Cloudflare (as a reverse proxy)

Cloudflare's CDN acts as a global reverse proxy. You point your DNS to Cloudflare, and they proxy traffic through their network, providing DDoS protection, caching, SSL, and WAF โ€” without running your own proxy server.

What Reverse Proxies Enable

1. SSL Termination

Handle SSL/TLS at the proxy, forward unencrypted traffic to backends on a private network. Simplifies backend configuration (no SSL certificates on each server) and improves performance (SSL handled once at the proxy).

2. Load Balancing

Distribute requests across multiple backend servers:

upstream backend {
    least_conn;  # Send to server with fewest connections
    server 10.0.0.1:3000 weight=3;  # Weighted (3x more requests)
    server 10.0.0.2:3000 weight=1;
    server 10.0.0.3:3000 backup;     # Only used if others down
}

server { location / { proxy_pass http://backend; } }

3. Caching

Cache responses at the proxy level, reducing backend load:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=MYCACHE:100m;
proxy_cache_key "$scheme$request_method$host$request_uri";

location / { proxy_cache MYCACHE; proxy_cache_valid 200 60m; # Cache 200 responses for 60 minutes proxy_cache_use_stale error timeout updating; # Serve stale if backend down proxy_pass http://backend; }

4. Compression

Gzip or Brotli compress responses before sending to clients:

gzip on;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1000;

5. Rate Limiting

Protect backends from abuse:

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;

location /api/ { limit_req zone=mylimit burst=20 nodelay; proxy_pass http://backend; }

6. Request/Response Modification

Add or remove headers, rewrite URLs, redirect traffic:

location /old-path {
    return 301 /new-path;
}

location /api/ { proxy_set_header X-API-Key "internal-key"; proxy_hide_header X-Powered-By; # Remove server info from response proxy_pass http://backend; }

7. Web Application Firewall (WAF)

Filter malicious requests before they reach your application. ModSecurity with Nginx, or Cloudflare's WAF for managed solutions.

Common Architectures

Single Server, Multiple Apps

One VPS running multiple applications on different ports:

                       โ”Œโ”€โ†’ Node.js (port 3000)
Reverse Proxy (80/443) โ”€โ”ผโ”€โ†’ Python (port 8000)
                       โ””โ”€โ†’ PHP-FPM (socket)
server {
    server_name app1.yourdomain.com;
    location / { proxy_pass http://127.0.0.1:3000; }
}

server { server_name app2.yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; } }

server { server_name yourdomain.com; location / { # PHP-FPM via socket fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } }

Docker/Microservice Architecture

Traefik or Nginx as the entry point, routing to Docker containers:

Internet โ†’ Reverse Proxy โ†’ Container A (frontend)
                         โ†’ Container B (API)
                         โ†’ Container C (auth service)

Global CDN with Origin Shield

Cloudflare (or similar) as the external reverse proxy, protecting your origin server:

Users โ†’ Cloudflare Edge (global) โ†’ Origin Server (your VPS)
        โ†‘ Caches content               โ†‘ Only handles cache misses
        โ†‘ DDoS protection              โ†‘ Protected from direct access

Security Benefits

A reverse proxy adds several layers of security:

Hides backend infrastructure. Clients only see the proxy โ€” they don't know what software, versions, or architecture lies behind it. Remove X-Powered-By headers and server tokens.

DDoS absorption. The proxy can absorb connection floods, rate-limit abusive IPs, and serve cached responses during attacks โ€” the backend never sees the load.

IP filtering. Block or allow specific IPs/ranges at the proxy level:

location /admin {
    allow 192.168.1.0/24;  # Office network
    deny all;
    proxy_pass http://backend;
}

Request filtering. Reject malformed or suspicious requests before they reach your application:

# Block requests with suspicious user agents
if ($http_user_agent ~* (scanner|bot|crawl|spider)) {
    return 403;
}

Zero-day protection. When vulnerabilities are discovered in backend software (Apache, PHP, Node.js), the proxy can filter or block exploit patterns while you patch โ€” without changing backend configuration.

Performance Considerations

Proxy overhead is minimal. Nginx adds <1ms of processing for proxied requests. The benefits (caching, compression, SSL offloading) far outweigh the micro-cost of proxying.

Connection pooling. The proxy can maintain persistent connections to backends, avoiding the TCP handshake overhead for each request:

upstream backend {
    server 127.0.0.1:3000;
    keepalive 64;  # Maintain 64 idle connections
}

Buffering. The proxy can buffer responses from slow backends, freeing up backend workers faster:

location / {
    proxy_buffering on;
    proxy_buffer_size 4k;
    proxy_buffers 8 16k;
    proxy_pass http://backend;
}

Common Reverse Proxy Configurations

Full Nginx Reverse Proxy for a Next.js App

server {
    listen 80;
    server_name yourdomain.com;
    
    # Redirect HTTP to HTTPS
    return 301 https://$host$request_uri;
}

server { listen 443 ssl http2; server_name yourdomain.com; ssl_certificate /etc/ssl/certs/yourdomain.crt; ssl_certificate_key /etc/ssl/private/yourdomain.key; # Security headers add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block"; add_header Referrer-Policy "strict-origin-when-cross-origin"; # Gzip gzip on; gzip_types text/plain text/css application/json application/javascript text/xml; # Proxy to Next.js location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; 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; proxy_cache_bypass $http_upgrade; } # Static asset caching location /_next/static { proxy_pass http://127.0.0.1:3000; proxy_cache_valid 200 365d; add_header Cache-Control "public, max-age=31536000, immutable"; } }

When You Don't Need a Reverse Proxy

A reverse proxy adds complexity. You don't need one if:

  • You have a single application on a single server with no caching requirements
  • Your application handles SSL/TLS directly and it works fine
  • You don't need load balancing, rate limiting, or request filtering
  • You're using a managed platform that handles proxying for you (Vercel, Netlify, Heroku)

However, even simple setups benefit from a reverse proxy for SSL termination and caching. The complexity cost is low (<30 lines of Nginx config), and the benefits accumulate as your setup grows.

The Quick Setup

For most self-hosted setups, Nginx as a reverse proxy is the right answer. The configuration is well-documented, the performance is excellent, and the feature set covers everything from SSL termination to caching to load balancing.

If you're running Docker: Traefik or Caddy. If you're building microservices: Traefik or Envoy. If you want a managed solution: Cloudflare. If you need advanced load balancing: HAProxy.

For everything else: Nginx.

Rather than DIY? Let OpsHelp handle everything.

Managed hosting with support, security, backups, and monitoring โ€” from ยฃ50/mo.

Get Managed Hosting โ†’

Need help with your server setup?

OpsHelp provides professional server management, setup, and hardening services.

Get Help from OpsHelp โ†’