Website Performance

What Is a Reverse Proxy? How It Works and Why You Need One

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

If you run more than one application on a server, or an application that listens on a port like 3000 or 8000, you will soon need a reverse proxy. It is a server that receives requests from the internet and forwards them to one or more backend services, then returns their responses to the client. Visitors only ever talk to the proxy; the applications behind it stay hidden. This article explains how a reverse proxy differs from a regular proxy, what it gives you, and how to set one up with Nginx.

Forward Proxy vs Reverse Proxy

Both sit in the middle of a connection, but they serve opposite sides:

Forward proxyReverse proxy
Acts on behalf ofClientsServers
Who configures itThe user or their networkThe site owner
Typical useFiltering, anonymity, corporate egressTLS, routing, caching, load balancing
The server seesThe proxy's IPThe client (via headers)

A forward proxy hides clients from the internet. A reverse proxy hides servers from clients.

What a Reverse Proxy Gives You

One Entry Point for Many Apps

A single server can route example.com to a WordPress site, api.example.com to a Node.js service and status.example.com to a monitoring dashboard, all on ports 80 and 443. The apps listen only on 127.0.0.1 and are not exposed directly.

Centralized TLS

Certificates are installed and renewed in one place. Backend apps speak plain HTTP on the local machine, which simplifies their configuration.

Protection From Slow Clients

Application servers often have a limited number of workers. A visitor on a slow mobile connection can hold a worker busy while the response trickles out. The proxy buffers the full response quickly and feeds it to the client at the client's pace, freeing the application.

Caching and Compression

The proxy can compress responses and cache them, so repeated requests never reach the application. Our website caching guide covers what to cache and for how long.

Load Balancing and Zero-Downtime Deploys

With several backend instances, the proxy spreads traffic between them and skips one that fails. You can take an instance out, update it and bring it back without visitors noticing.

Security Controls

Rate limiting, IP allow lists, request size limits and security headers can be enforced before traffic reaches the application.

Setting Up Nginx as a Reverse Proxy

Assume a Node.js app listening on 127.0.0.1:3000. Create /etc/nginx/sites-available/app.conf:

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        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;
    }
}

Enable it and reload:

sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Then add HTTPS with Certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com

Proxying WebSockets

Real-time apps need two extra headers so the connection can be upgraded:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Load Balancing Between Instances

upstream app_backend {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
}

server {
    listen 80;
    server_name app.example.com;
    location / {
        proxy_pass http://app_backend;
    }
}

Nginx uses round robin by default; least_conn sends new requests to the instance with the fewest active connections.

Common Pitfalls

  • Every visitor appears as 127.0.0.1. The app must read the client IP from X-Forwarded-For or X-Real-IP. Configure your framework to trust the proxy, and trust only the proxy, otherwise clients can spoof the header.
  • Redirect loops after enabling HTTPS. The app thinks the request is plain HTTP and redirects again. Pass X-Forwarded-Proto and make the app respect it.
  • 502 Bad Gateway. The backend is down or listening on a different address. Check with curl -I http://127.0.0.1:3000 and ss -tlnp.
  • 504 Gateway Timeout. A slow backend exceeds proxy_read_timeout (60 seconds by default). Fix the slow request first; raise the timeout only for routes that genuinely need it.
  • Upload failures. Large uploads hit client_max_body_size, which defaults to 1 MB.

Other Reverse Proxy Options

Nginx is the most widely documented, but it is not the only choice. Caddy offers automatic HTTPS with a one-line reverse_proxy directive, HAProxy is focused on load balancing with detailed health checks, and Traefik discovers Docker containers automatically. A CDN is also a reverse proxy, just one running in many locations. For background on the server software itself, read what a web server is.

FAQ

Does a reverse proxy slow down my site?

On the same machine the extra hop is local and very fast. In practice a well-configured proxy often makes sites faster because of compression, caching, connection reuse and buffering for slow clients.

Is a reverse proxy the same as a load balancer?

A load balancer is one job a reverse proxy can do. Every load balancer for web traffic is a reverse proxy, but a reverse proxy in front of a single app is not balancing anything; it is still useful for TLS, routing and caching.

Can a reverse proxy protect against DDoS attacks?

It helps against small floods through rate limits and connection limits, but it runs on the same server and network link as your app. Large volumetric attacks need filtering upstream, at the provider or a dedicated protection service.

Related guides