Networking

What Is a Reverse Proxy? How It Works, With Nginx and Caddy

A reverse proxy is a server that sits in front of one or more web servers, receives requests from clients as if it were the website, and forwards them to the right backend. Clients only ever talk to the proxy; the backends stay hidden behind it.

The minimal setup is one line of Caddy (reverse_proxy localhost:8080) or an nginx location block with proxy_pass http://127.0.0.1:8080;, both shown in full below.

What a reverse proxy does, in one request

  1. A browser resolves example.com and connects to the reverse proxy's public IP on port 443.
  2. The proxy completes the TLS handshake, reads the HTTP request, and picks a backend based on rules: hostname, path, or a load-balancing policy.
  3. It opens its own connection to the backend (for example http://127.0.0.1:8080) and sends the request, usually adding headers that say who the original client was.
  4. It relays the backend's response to the browser, possibly caching or compressing it on the way.

The HTTP standard calls this kind of intermediary a gateway. RFC 9110 defines a "gateway" (a.k.a. "reverse proxy") as "an intermediary that acts as an origin server for the outbound connection but translates received requests and forwards them inbound to another server or servers."

Forward proxy vs reverse proxy

The difference is who chose the proxy and whom it works for.

Forward proxyReverse proxy
Chosen byThe client (browser or OS proxy settings)The website operator
Acts on behalf ofClients going out to many sitesServers receiving traffic for one site or service
Client knows it existsYes, it is configured on the clientNo, it looks like the website itself
Typical useCorporate web filtering, caching outbound trafficTLS termination, load balancing, routing to apps

RFC 9110 describes a forward proxy as "a message-forwarding agent that is chosen by the client, usually via local configuration rules". The reverse proxy is the mirror image: configured on the server side, invisible to the client.

Why put a reverse proxy in front of an app

  • TLS termination. The proxy holds the certificate and handles HTTPS, so each app behind it can speak plain HTTP on a private interface.
  • Several apps on one IP and port. One proxy on port 443 can route api.example.com to one process and example.com/blog/ to another.
  • Load balancing. Spread requests over several copies of an app and stop sending traffic to one that fails.
  • Hiding and protecting backends. The app's port is never exposed to the internet; rate limits, request size limits and access rules sit in the proxy.
  • Caching and compression of responses, so the app does less work. A CDN is this idea spread across many cities.

Reverse proxy with nginx

This forwards everything on example.com to an app on port 8080 of the same machine:

server {
    listen 80;
    server_name example.com;

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

The proxy_set_header lines matter because of nginx's defaults. Out of the box nginx sends Host: $proxy_host (the backend's address, here 127.0.0.1:8080) and Connection: close. Apps that build links or pick a site from the Host header then see the wrong host, so pass the original with Host $host. $proxy_add_x_forwarded_for is the incoming X-Forwarded-For header with the client's address appended, or just the client's address if there was no such header.

The trailing slash in proxy_pass

Whether proxy_pass ends in a URI changes the path the backend receives. This is the most common nginx proxy surprise:

ConfigRequestBackend receives
location /app/ { proxy_pass http://127.0.0.1:8080; }/app/users/app/users
location /app/ { proxy_pass http://127.0.0.1:8080/; }/app/users/users

The nginx docs put it this way: with a URI in proxy_pass (even just /), the part of the request path matching the location is replaced by that URI; without one, the path is passed unchanged.

WebSockets through nginx

Upgrade and Connection are hop-by-hop headers, so nginx does not forward them unless told to. The nginx WebSocket docs give this pattern:

location /chat/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

As of nginx 1.29.7, HTTP/1.1 is the default for proxied requests; on older versions the default was 1.0 and the proxy_http_version 1.1; line is required. Keeping it is harmless on newer versions.

Load balancing in nginx

upstream app {
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
}

server {
    listen 80;
    location / {
        proxy_pass http://app;
    }
}

By default nginx distributes requests "using a weighted round-robin balancing method". A server is marked unavailable after 1 failed attempt (max_fails) for 10 seconds (fail_timeout) unless you change those parameters.

Reverse proxy with Caddy

example.com {
    reverse_proxy localhost:8080
}

Because a domain name is the site address, Caddy's automatic HTTPS obtains a certificate for example.com and keeps it renewed. Per the Caddy docs, it passes the incoming Host header through unchanged, sets or appends X-Forwarded-For, and sets X-Forwarded-Proto and X-Forwarded-Host. It also ignores X-Forwarded-* values arriving from clients unless you list the sender in trusted_proxies, which prevents spoofing. With several backends listed, the default load-balancing policy is random.

Getting the real client IP behind a proxy

Behind a reverse proxy, every connection to your app comes from the proxy, so the app's logs and rate limiter see the proxy's address (often 127.0.0.1). The proxy passes the client address in X-Forwarded-For, which looks like this after two proxies (example values from MDN):

X-Forwarded-For: 203.0.113.195, 2001:db8:85a3:8d3:1319:8a2e:370:7348, 198.51.100.178

The leftmost entry claims to be the client, and each proxy appends the address it received the request from. MDN's warning is the key point: any part of the header not added by a proxy you trust "may be spoofed", and if your server is reachable directly from the internet, "no part" of the list is trustworthy. A client can send X-Forwarded-For: 1.2.3.4 itself. For anything security-related (rate limits, IP allowlists), read the list from the right and take the first address not added by your own proxies, and configure your framework's "trusted proxy" setting to match.

The standardised alternative is the Forwarded header, though MDN notes it is used much less often.

If the proxy returns 502 Bad Gateway

A 502 from nginx or Caddy means the proxy could not get a valid response from the backend. Check, in order: that the app is running and listening on the address in proxy_pass (on Linux, ss -ltnp; for Docker, that the port is published to the host); that the app listens on 127.0.0.1 or 0.0.0.0 as expected rather than only inside a container; and the proxy's error log, which names the upstream address it failed to reach and why. HTTP status codes explained covers 502 against 503 and 504, and Docker port mapping covers the container case.

Frequently asked questions

Is a load balancer a reverse proxy?
An HTTP load balancer is a reverse proxy whose main job is spreading requests across several backend servers. nginx and Caddy can both do it: nginx distributes requests with weighted round-robin by default, and Caddy picks a backend at random by default.
Is a VPN a reverse proxy?
No. A VPN carries all of a device's traffic through a tunnel and is chosen by the user, which makes it closer to a forward proxy. A reverse proxy is set up by the website operator and only handles requests for that site.
Why does my app log 127.0.0.1 for every visitor?
Because the reverse proxy is the one opening the connection to your app. Pass the client address in a header such as X-Forwarded-For or X-Real-IP from the proxy, and configure your app or framework to trust that header only when the request comes from the proxy's address.
Should the app still listen on a public port behind a reverse proxy?
No. Bind the app to 127.0.0.1 or a private network address so it is only reachable through the proxy. If the app is reachable directly, anyone can bypass the proxy's TLS, rate limits and access rules, and can also send forged X-Forwarded-For headers.

Sources