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.
- Updated Nov 27, 2026
- Markdown
Explain what a reverse proxy does and write a three-line Caddyfile that serves an app over HTTPS.
What a reverse proxy does
- Every visitor reaches the proxy, the only service open to the internet.
- The proxy holds the certificate and ends the HTTPS connection.
- Requests for the site go to the web app on its private port.
- The web app answers the proxy, not the visitor.
- A request for another path arrives at the same address.
- The proxy forwards it to the API app, so many apps share one domain.
Static diagram
- Visitorexample.comReverse proxy
- Reverse proxyTLSVisitor
- Reverse proxy/Web app
- Web apppageReverse proxy
- Visitor/apiReverse proxy
- 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.
https://app.example.com -> proxy -> localhost:3000
https://api.example.com -> proxy -> localhost:4000A 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:
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#
| Proxy | Certificates | Config style | Good fit |
|---|---|---|---|
| Caddy | Automatic | Short Caddyfile | Small servers, quick HTTPS |
| nginx | With certbot or another tool | Server blocks | Familiar, very widely documented |
| Traefik | Automatic | Labels or files | Docker setups that change often |
| HAProxy | External | Frontend and backend rules | Heavy 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:
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-Forand 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. PassX-Forwarded-Protoand 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
UpgradeandConnectionheaders. - 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:3000opens the app to the world. Publish to127.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#
| Symptom | Likely cause | Fix |
|---|---|---|
502 Bad Gateway | Proxy cannot reach the app | Start 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 address | Check ss -tlnp and the port in proxy_pass |
504 Gateway Time-out | App answers too slowly | Fix 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 80 | Stop one of them, then start the proxy |
| Browser shows a certificate warning | Domain does not point here or issuance failed | Check DNS, ports 80 and 443 and the proxy logs |
| App logs show 127.0.0.1 for everyone | Forwarded headers not trusted | Configure the app’s trusted proxy setting |
| Redirect loop between http and https | App and proxy disagree about the scheme | Pass X-Forwarded-Proto and trust it in the app |
| Wrong site appears for a domain | Name not matched by any block | Add 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#
# Caddyfile: one site, one app
app.example.com {
reverse_proxy localhost:3000
}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 outsideSecurity and safety notes#
- Keep app ports bound to
127.0.0.1or a private Docker network. - Only trust forwarded headers from your own proxy.
- Do not expose admin dashboards through the proxy without authentication.
Related lessons#
- Nginx reverse proxy with certbot: the same idea with nginx.
- Automatic HTTPS with Caddy: how Caddy gets certificates.
- Publish container ports safely: keep app ports off the internet.
- Fix 502 Bad Gateway: when the proxy cannot reach the app.
Next#
Next, see how a proxy lets you swap versions without a gap in Zero-downtime deploys.
Check what you learned
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
Keep going: guides and tutorials
- Zero Downtime Deploy for Docker Apps, With Rollback How a Docker deploy waits for readiness before cutover, holds the previous release for rollback, rolls back on its own, and how to rehearse it.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.