Chapter 4 · Lesson 1 of 18
Deploy and operate

What is a reverse proxy? Explained with Caddy and nginx

A reverse proxy sits in front of your apps, accepts requests from the internet and forwards each one to the right app, handling HTTPS on the way.

By the end you will

Explain what a reverse proxy does and write a three-line Caddyfile that serves an app over HTTPS.

Time
16 min
Level
Beginner
Check
4 questions

What a reverse proxy does

A reverse proxy takes requests on ports 80 and 443, reads the domain, ends HTTPS and forwards each request to the right app on a private port.
  1. Every visitor reaches the proxy, the only service open to the internet.
  2. The proxy holds the certificate and ends the HTTPS connection.
  3. Requests for the site go to the web app on its private port.
  4. The web app answers the proxy, not the visitor.
  5. A request for another path arrives at the same address.
  6. The proxy forwards it to the API app, so many apps share one domain.
Static diagram
  1. Visitorexample.comReverse proxy
  2. Reverse proxyTLSVisitor
  3. Reverse proxy/Web app
  4. Web apppageReverse proxy
  5. Visitor/apiReverse proxy
  6. Reverse proxyforwardAPI app

A reverse proxy is a server that receives web requests on behalf of your apps and forwards each one to the right app behind it. On a single VPS it is what lets https://app.example.com and https://api.example.com share ports 80 and 443 while the apps themselves listen on private ports.

A short history#

Reverse proxies grew out of caching and load balancing for busy sites in the 1990s and 2000s, and servers such as nginx and HAProxy made them common. Newer servers like Caddy added automatic HTTPS, which is why a small proxy config can now be three lines.

How it works#

Only one program can listen on port 443 of an IP address. The proxy owns ports 80 and 443. When a request arrives, the proxy reads the Host header (and for HTTPS, the SNI name during the handshake), picks a matching rule, and opens its own connection to the app. The app answers the proxy, and the proxy relays the answer to the visitor.

output
https://app.example.com  ->  proxy  ->  localhost:3000
https://api.example.com  ->  proxy  ->  localhost:4000

A worked example, start to finish. A visitor types https://app.example.com. DNS returns your server’s IP address. The browser connects to port 443 and sends the name app.example.com. The proxy picks that name’s certificate, finishes the HTTPS handshake, reads the request GET /, and sends it as plain HTTP to localhost:3000. The app returns a page. The proxy sends it back, adding headers if you ask it to. The app never touched a certificate, and port 3000 was never exposed to the internet.

Because the proxy is the client from the app’s point of view, it passes the original details along in headers: Host, X-Forwarded-For for the visitor’s address and X-Forwarded-Proto for whether the visitor used HTTPS. Apps behind a proxy must be told to trust these, or they will log the proxy’s address and think every request is plain HTTP.

A minimal example with Caddy#

A Caddyfile:

output
app.example.com {
    reverse_proxy localhost:3000
}

Caddy gets a certificate for the domain and forwards traffic. The domain must point at the server and ports 80 and 443 must be open. The same job in nginx takes a server block plus certbot.

Comparing the common choices#

ProxyCertificatesConfig styleGood fit
CaddyAutomaticShort CaddyfileSmall servers, quick HTTPS
nginxWith certbot or another toolServer blocksFamiliar, very widely documented
TraefikAutomaticLabels or filesDocker setups that change often
HAProxyExternalFrontend and backend rulesHeavy load balancing

What a proxy also does#

  • Serves many domains from one server.
  • Ends HTTPS so your apps do not have to.
  • Can switch traffic from an old release to a new one, which is how zero-downtime deploys work.
  • Adds limits, compression, caching headers and access logs in one place.

Common misconceptions#

  • A proxy does not make an app secure. It hides the app port, but a bug in the app is still reachable through the proxy.
  • HTTPS to the proxy does not mean HTTPS to the app. The hop on localhost is usually plain HTTP, which is fine on one machine and not fine across a network you do not control.
  • A reverse proxy is not a load balancer by default. It becomes one when you list several backends.
  • A proxy in front of the app does not remove the need for a firewall.

When not to use it#

For a static site with no server process, the web server itself can serve the files. For a purely internal tool reachable by IP on a private network, a proxy adds little. And if your platform already provides ingress, adding a second proxy only adds a hop to debug.

Check it worked#

From the server, see what listens on the public ports and that the app port is private:

bash
sudo ss -tlnp | grep -E ':80|:443|:3000'

You should see the proxy on 0.0.0.0:80 and 0.0.0.0:443 (or *:80 and *:443), and the app on 127.0.0.1:3000. Then curl -I https://app.example.com should return the app’s response with no certificate error, while curl -I http://203.0.113.10:3000 from another machine should fail to connect.

Edge cases and gotchas#

  • The app sees the wrong client IP. Every request seems to come from 127.0.0.1. The proxy must pass X-Forwarded-For and the app must trust it, but only from the proxy, or visitors can spoof it.
  • The app thinks it is on HTTP. It generates http:// links or redirect loops. Pass X-Forwarded-Proto and configure the app to honor it.
  • Websockets and streaming break. Some proxies need extra settings for upgrade requests. Caddy handles websockets by default. nginx needs Upgrade and Connection headers.
  • Large uploads or slow requests fail. Proxies have body size and timeout limits. nginx returns 413 for oversized bodies and 504 when the app takes too long.
  • A published Docker port skips the proxy. -p 3000:3000 opens the app to the world. Publish to 127.0.0.1:3000:3000, or keep the app on a Docker network with no published port.
  • Trailing slashes change paths in nginx. proxy_pass http://127.0.0.1:3000; passes paths unchanged, while a URI after the port rewrites them.
  • The default site answers instead of yours. A request for an unknown name lands on the first or default server block. Match the exact server_name.

Troubleshooting#

SymptomLikely causeFix
502 Bad GatewayProxy cannot reach the appStart the app and confirm its port. See the 502 lesson
connect() failed (111: Connection refused) while connecting to upstream (nginx)Nothing listens at the upstream addressCheck ss -tlnp and the port in proxy_pass
504 Gateway Time-outApp answers too slowlyFix the slow request or raise the proxy read timeout
bind() to 0.0.0.0:80 failed (98: Address already in use)Two programs want port 80Stop one of them, then start the proxy
Browser shows a certificate warningDomain does not point here or issuance failedCheck DNS, ports 80 and 443 and the proxy logs
App logs show 127.0.0.1 for everyoneForwarded headers not trustedConfigure the app’s trusted proxy setting
Redirect loop between http and httpsApp and proxy disagree about the schemePass X-Forwarded-Proto and trust it in the app
Wrong site appears for a domainName not matched by any blockAdd that name to the right site block

Real-world scenario#

You host a blog on localhost:2368 and a small API on localhost:4000, with both domains pointed at 203.0.113.10. Before the proxy, you opened both ports, and a friend noticed that http://203.0.113.10:4000 worked without HTTPS.

You close both ports in the firewall, install Caddy and write two site blocks, one per domain, each with a reverse_proxy line. You reload Caddy and watch journalctl -u caddy until both certificates are obtained. Now https://blog.example.com and https://api.example.com both work, and the raw ports no longer answer from outside.

A week later the API starts returning 502 after a reboot. Caddy is fine: the API’s systemd unit was not enabled. You enable it, the proxy finds it on localhost:4000 again, and the error goes away. The proxy told you the truth: it was up, the app was not. See fix 502 Bad Gateway for a full checklist, and deploy Express on a VPS with Docker for a similar setup.

Quick reference#

output
# Caddyfile: one site, one app
app.example.com {
    reverse_proxy localhost:3000
}
bash
sudo ss -tlnp | grep -E ':80|:443'      # who holds the public ports
sudo systemctl reload caddy              # apply a Caddyfile change
sudo nginx -t && sudo systemctl reload nginx   # same for nginx
curl -I https://app.example.com          # check from outside

Security and safety notes#

  • Keep app ports bound to 127.0.0.1 or a private Docker network.
  • Only trust forwarded headers from your own proxy.
  • Do not expose admin dashboards through the proxy without authentication.

Next#

Next, see how a proxy lets you swap versions without a gap in Zero-downtime deploys.

Check what you learned4 questions
1What does a reverse proxy do with a request for app.example.com?
2Why not expose each app's port directly to the internet?
3How does the Caddyfile example get a certificate for app.example.com?
4Your app logs show every visitor as 127.0.0.1. What is the cause?

Frequently asked questions

Why not expose each app's port directly?

You would need every app to handle HTTPS itself and visitors would type port numbers. A proxy gives each app a clean domain and one place for certificates.

What is the difference between a reverse proxy and a forward proxy?

A forward proxy sits in front of clients and fetches sites on their behalf. A reverse proxy sits in front of servers and receives requests on their behalf. Visitors usually do not know a reverse proxy is there.

Why does my app see the proxy's IP instead of the visitor's IP?

The app's connection comes from the proxy. The proxy passes the real address in a header such as X-Forwarded-For, and the app must be set to trust and read it.

Do I need a reverse proxy for one app on one server?

Not strictly, but it is still the simplest way to get HTTPS on ports 80 and 443, to keep the app port private and to add a second app later without changes.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios