[Blog](https://deploywithox.com/blog) [Guides](https://deploywithox.com/blog#c-guides) # Caddy Reverse Proxy 502: Fix dial tcp Connection Refused Fix a Caddy reverse proxy 502 with dial tcp connection refused: read the failing request's log line, compare where Caddy dials with where the app listens. Saurav Sharma Dec 1, 2026 14 min read On this page - [What the 502 and "connection refused" tell you](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-meaning) - [Check a running server](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-diagnose) - [Fix the port and reload Caddy](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-fix) - [Reproduce it safely on your own machine](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-lab) - [Same port, wrong address: 127.0.0.1 and ::1](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-address) - [When the app is not running](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-down) - [Clean up the lab](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-cleanup) - [How ox keeps the port and the proxy in step](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-ox) - [Key takeaways](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#key-takeaways) - [Frequently asked questions](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#faq) - [Next step](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-next) When Caddy's reverse proxy answers 502 Bad Gateway and Caddy's log line for that request says `dial tcp 127.0.0.1:8001: connect: connection refused`, the browser reached Caddy, and Caddy's own connection to the app was refused. The first thing to check is whether anything listens at that exact address and port. This post walks through three ways that fails: the `reverse_proxy` line names one port and the app listens on another, the app listens on another address, or the app is not running. If you have a live 502 now, go straight to [the checks on a running server](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-diagnose). Further down, an optional lab reproduces the fault on your machine's loopback address and removes everything it made. This post covers Caddy and the app running directly on one host, with no containers. How it was tested: the server checks ran as root on a fresh Ubuntu 26.04 server with Caddy v2.11.9 from Caddy's own apt repository. The lab blocks ran as an ordinary user with no sudo, on Ubuntu 24.04 with the Caddy v2.11.9 release binary, Python 3.12 and curl 8.5. Every output below comes from those runs. I build ox, which puts Caddy in front of every app it deploys. You need no ox for any of this, and the end says what ox does about it. ## What the 502 and "connection refused" tell you A request passes two hops: the browser reaches Caddy, and Caddy opens a second connection to the app, which Caddy's docs call the upstream. A 502 from Caddy says the first hop worked and the second did not. The 502 alone does not say why. The log line Caddy writes for that request does. In this example the second hop fails: Caddy dials port 8001, and the app listens on port 8000. "Connection refused" is the error the Linux [connect(2) manual](https://man7.org/linux/man-pages/man2/connect.2.html) describes as finding "no one listening on the remote address". A firewall rule that rejects the connection can give the same error, so an empty port is the first thing to check, not a verdict. The error says nothing about the app's code. The question is where the app listens, if it runs at all. Caddy's [apt package](https://caddyserver.com/docs/install#debian-ubuntu-raspbian) runs Caddy as a systemd service named `caddy`, and its output goes to the journal, as the [Caddy docs on running it](https://caddyserver.com/docs/running) describe. On the test server, with no `log` directive in the Caddyfile, each 502 left one line with `"level":"error"` in the journal, and its `msg` field held the address Caddy dialed. ## Check a running server Look before you change anything. These checks run on the server itself and only read. **1. Make a new request and read its error.** Ask for the failing page once, with your own site's address: Shell ```bash curl -sS -o /dev/null -w '%{http_code}\n' http://localhost/ ``` Then read the newest error line in the journal: Shell ```bash sudo journalctl -u caddy --no-pager -n 50 -o cat | grep '"level":"error"' | tail -n 1 | grep -o '"msg":"[^"]*"' ``` Code ```text "msg":"dial tcp 127.0.0.1:8001: connect: connection refused" ``` Drop the last `grep -o` to see the whole line: check that its `ts` (Unix seconds) is the time of your request and its `request.host` is your site. An older line can come from a fault that is already gone, or from another site on the same Caddy. **2. Where Caddy dials now.** The [admin API](https://caddyserver.com/docs/api) exports the config the running Caddy uses, at the [default admin address](https://caddyserver.com/docs/caddyfile/options#admin) `localhost:2019` unless the `admin` option or the `CADDY_ADMIN` variable sets another. `caddy adapt` turns the file on disk into the same JSON. Compare the two: Shell ```bash curl -fsS http://localhost:2019/config/ | python3 -m json.tool caddy adapt --config /etc/caddy/Caddyfile --pretty ``` In each output, follow the route matching the failing host and path to its reverse proxy handler. Compare the upstream `dial` values for that route. A difference can mean a reload failed or the edited file was never loaded. `sudo systemctl status caddy --no-pager` and the journal show the last reload and its result. Compare the running route’s address with the address in the fresh error. A config that passes validation can still name the wrong port. **3. Where the app listens.** `ss` lists listening TCP sockets with the program that owns each: Shell ```bash sudo ss -ltnp | grep -E ':(80|8000|8001)\b' ``` Code ```text LISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=2394,fd=3)) LISTEN 0 4096 *:80 *:* users:(("caddy",pid=2176,fd=10)) ``` The `grep` checks the proxy port, the app’s expected port and the refused port. Substitute the ports from your configuration and journal. Keep the `sudo`: run by an ordinary user on the test server, the same command left the last column empty for both lines, because the app and Caddy ran as other users. Here the app is on 8000, and nothing is on 8001. **4. Ask the app directly, past Caddy, right away.** Use the address and port from `ss`: Shell ```bash curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/ ``` What the checks mean together: | What you see | What to look at | | --- | --- | | The app's port in `ss` differs from the running `dial` | The port in one of them. Decide which is right and change the other. | | Same port, different address, such as `[::1]:8000` against `127.0.0.1:8000` | The address. See the section on 127.0.0.1 and ::1 below. | | Nothing listening on the app's port | The app is stopped or crashing. Read `systemctl status` and the journal of the app's own service. | | The running `dial` differs from the file on disk | The last reload. Read why it failed before you reload again. | | The app answers on the dialed address, and a new request still logs "connection refused" there | Repeat both checks together: the app may have come back between them. Look in `sudo ss -ltnp` for a second `caddy` process holding your site's port. In [one forum thread](https://caddy.community/t/caddy-fails-with-502-on-half-my-reverse-proxy-entries/31622), an old instance that never stopped kept answering. Also check for a firewall rule on loopback. | ## Fix the port and reload Caddy If the app is right and the Caddyfile is wrong, edit `/etc/caddy/Caddyfile`, then validate and reload in one line, so a file that fails validation never reaches the reload: Shell ```bash caddy validate --config /etc/caddy/Caddyfile && sudo systemctl reload caddy && echo reloaded ``` The Caddy docs give `systemctl reload caddy` as the graceful reload, and warn against stopping the service to change the config, since that takes the site down. On the test server, a reload of a Caddyfile with an unknown directive failed with "Job for caddy.service failed", the service stayed active, and the config it already had kept serving. Validating first keeps you from relying on that. Then make a new request and read its line again, as in check 1. If the app was the wrong side, change the app's port instead and restart its service. Then repeat the checks: the running dial address, the listening socket and a direct `curl` should all agree. ## Reproduce it safely on your own machine This lab is optional. It runs a small app and its own Caddy as your ordinary user, with no sudo, on loopback addresses only, so nothing outside your machine can reach it. Its Caddy has its own admin address, 127.0.0.1:2020, so it leaves alone any Caddy you already run. You need `caddy`, `python3`, `curl` and `ss`, and ports 8000, 8001, 8080 and 2020 free. The setup checks the ports and starts nothing if one is taken. Start a separate shell for the lab. The folder change and the variables below end with it: Shell ```bash bash ``` Then paste the setup into that shell. The app is Python's built-in web server on port 8000. Caddy is told port 8001, which is the mistake. `listening` waits up to five seconds for a port to open, and `lab_up` stops at the first step that fails: Shell ```bash APP= CADDY= LAB= listening() { for _ in $(seq 50); do ss -ltnH "sport = :$1" | grep -q . && return 0; sleep 0.1; done; echo "nothing listens on port $1"; return 1; } lab_up() { if ss -ltnH | grep -qE ':(8000|8001|8080|2020)\b'; then echo "a lab port is taken"; return 1; fi LAB=$(mktemp -d -t caddy502-lab.XXXXXX) || return 1 cd "$LAB" || return 1 export XDG_CONFIG_HOME="$LAB/config" XDG_DATA_HOME="$LAB/data" mkdir site && echo "hello from the app" > site/index.html || return 1 python3 -m http.server 8000 --bind 127.0.0.1 --directory site > app.log 2>&1 & APP=$! cat > Caddyfile <<'EOF' { admin 127.0.0.1:2020 } http://127.0.0.1:8080 { bind 127.0.0.1 reverse_proxy 127.0.0.1:8001 } EOF caddy run --config Caddyfile > caddy.log 2>&1 & CADDY=$! listening 8000 && listening 8080 } lab_up && echo "lab is up" ``` Code ```text lab is up ``` If it prints anything else, run the [cleanup](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-cleanup): it is safe after a setup that stopped part way. The two `XDG_` variables keep the files Caddy writes inside the lab folder. Caddy listens on all interfaces unless told otherwise, so [`bind 127.0.0.1`](https://caddyserver.com/docs/caddyfile/directives/bind) keeps the test site on loopback. Ask it for the page: Shell ```bash curl -i http://127.0.0.1:8080/ ``` Code ```text HTTP/1.1 502 Bad Gateway Server: Caddy Date: Tue, 01 Dec 2026 20:54:29 GMT Content-Length: 0 ``` The error line in the lab Caddy's log, which on a server would be in the journal: Shell ```bash grep '"level":"error"' caddy.log ``` Code ```text {"level":"error","ts":1796158469.2501822,"logger":"http.log.error","msg":"dial tcp 127.0.0.1:8001: connect: connection refused","request":{"remote_ip":"127.0.0.1","remote_port":"46272","client_ip":"127.0.0.1","proto":"HTTP/1.1","method":"GET","host":"127.0.0.1:8080","uri":"/","headers":{"User-Agent":["curl/8.5.0"],"Accept":["*/*"]}},"duration":0.0003423,"status":502,"err_id":"53uayajkq","err_trace":"reverseproxy.statusError (reverseproxy.go:1776)"} ``` Now the checks from above, in lab form. The running config, from the lab's admin address: Shell ```bash curl -sS http://127.0.0.1:2020/config/ | grep -o '"dial":"[^"]*"' ``` Code ```text "dial":"127.0.0.1:8001" ``` The listening sockets. The lab's programs are yours, so `ss` names them without sudo: Shell ```bash ss -ltnp | grep -E ':(8000|8001|8080|2020)\b' ``` Code ```text LISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=1979502,fd=3)) LISTEN 0 4096 127.0.0.1:8080 0.0.0.0:* users:(("caddy",pid=1979505,fd=7)) LISTEN 0 4096 127.0.0.1:2020 0.0.0.0:* users:(("caddy",pid=1979505,fd=4)) ``` Caddy and the app listen only on 127.0.0.1, and nothing is on 8001. The app, asked directly on both ports: Shell ```bash curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8001/ curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/ ``` Code ```text curl: (7) Failed to connect to 127.0.0.1 port 8001 after 0 ms: Couldn't connect to server 000 200 ``` Fix the Caddyfile, validate it, and reload only if it is valid. `caddy reload` finds the lab Caddy because it takes the admin address from the file: Shell ```bash sed -i 's/127.0.0.1:8001/127.0.0.1:8000/' Caddyfile caddy validate --config Caddyfile && caddy reload --config Caddyfile && curl -i http://127.0.0.1:8080/ ``` Both Caddy commands also print a few JSON lines at the info level, left out here: Code ```text Valid configuration HTTP/1.1 200 OK Content-Length: 19 Content-Type: text/html Date: Tue, 01 Dec 2026 20:54:29 GMT Last-Modified: Tue, 01 Dec 2026 20:54:29 GMT Server: SimpleHTTP/0.6 Python/3.12.3 Via: 1.0 Caddy hello from the app ``` The `Via: 1.0 Caddy` header shows the answer came from the app through Caddy. You can stop here and jump to [cleanup](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-cleanup), or go on to the next two faults. ## Same port, wrong address: 127.0.0.1 and ::1 A port can match and the address still differ. `127.0.0.1` is the IPv4 loopback and `::1` the IPv6 one, and a program listening on one does not answer on the other. In the lab shell, restart the app on `::1` only: Shell ```bash kill "$APP"; wait "$APP"; APP= python3 -m http.server 8000 --bind ::1 --directory site > app.log 2>&1 & APP=$! listening 8000 curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/ grep '"level":"error"' caddy.log | tail -n 1 | grep -o '"msg":"[^"]*"' ss -ltnp | grep ':8000\b' ``` Code ```text 502 "msg":"dial tcp 127.0.0.1:8000: connect: connection refused" LISTEN 0 5 [::1]:8000 [::]:* users:(("python3",pid=1979582,fd=3)) ``` An interactive shell also prints a `Terminated` line for each `kill`, left out here. Caddy still dials `127.0.0.1:8000`, and the app listens on `[::1]:8000`. Make both sides name the same address. Put the app back on `127.0.0.1`: Shell ```bash kill "$APP"; wait "$APP"; APP= python3 -m http.server 8000 --bind 127.0.0.1 --directory site > app.log 2>&1 & APP=$! listening 8000 && curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/ ``` Code ```text 200 ``` The name `localhost` can cause the same mismatch. On the Ubuntu 26.04 test server it resolved to both `127.0.0.1` and `::1`, and the same Python server started with `--bind localhost` listened on `[::1]:8000` only. Write the number on both sides. Keep the app on loopback too: an app on `0.0.0.0` answers Caddy, and also anyone who can reach that port from outside, past Caddy and its HTTPS. ## When the app is not running A stopped app gives the same 502 and the same log line, because nothing listens. In the lab shell: Shell ```bash kill "$APP"; wait "$APP"; APP= curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/ ``` Code ```text 502 ``` On a server, `systemctl status` for the app's service says whether it runs, and its journal says why it stopped. If the 502s come only for a few seconds during each restart, Caddy can hold requests while the app comes back. The [reverse_proxy docs](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#lb_try_duration) describe `lb_try_duration`, which keeps trying a backend that is down for the time you give it. It is off by default. Code ```text reverse_proxy 127.0.0.1:8000 { lb_try_duration 5s } ``` On the test server, with the packaged Caddy and this setting, a request with the app stopped got its 502 after 5.0 seconds instead of in about a millisecond. When the app started two seconds into the wait, the same request got a 200 after 2.3 seconds. That hides a short gap. It does not fix a wrong port, which would then show up as slow 502s. ## Clean up the lab Run this in the lab shell, from any folder, whether or not you did every section. It stops what the lab started, waits for both to exit, and removes only the folder setup made: Shell ```bash for pid in ${APP:-} ${CADDY:-}; do kill "$pid" && wait "$pid"; done; APP= CADDY= case "${LAB:-}" in */caddy502-lab.*) cd / && rm -r -- "$LAB" && echo "removed $LAB" && LAB= ;; *) echo "LAB does not name the lab folder, nothing removed" ;; esac ``` Then leave the lab shell, which puts back your own folder and `XDG_` values: Shell ```bash exit ``` Back in your own shell, check that nothing of the lab still listens: Shell ```bash ss -ltn | grep -E ':(8000|8001|8080|2020)\b' || echo "no test listeners left" ``` Code ```text no test listeners left ``` A Caddy service you already had keeps running untouched. ## How ox keeps the port and the proxy in step ox deploys each app as a plain systemd service behind Caddy, with no Docker. For an HTTP app, ox picks the port unless you pin one. The app gets it in `$PORT`, and ox points Caddy at `127.0.0.1` and that port, so the port lives in one place. Some servers ignore `PORT` and use their own default, and `ox check` refuses such a start command before anything runs. For a FastAPI repo started with plain `uvicorn main:app`, it printed: Code ```text Problems: - app.start: uvicorn listens on port 8000 unless told, and ox gives the app its port in $PORT, so the health check would find nothing: add --host 127.0.0.1 --port $PORT to the command ox check: 1 problem to fix before this repo can deploy ``` With `--host 127.0.0.1 --port $PORT` added, it said "Ready to deploy." When a healthy release is already live on a port ox picked, a deploy starts the new release beside it on a second port and asks it for a page for up to 120 seconds. With no health path set, any status but 502, 503 or 504 counts as ready. Caddy is switched to the new release only after a check succeeds. If none does, the deploy fails, the live release keeps serving, and the run's log names the failed step. Two cases restart the app in place instead, a pinned port and `stop_before_start`, and the site can answer 502 until the new release answers. A first deploy has no previous release to fall back on. The [[app] section of the config docs](https://deploywithox.com/docs/config#app) covers the port, the servers that need their port option and `stop_before_start`, and [troubleshooting](https://deploywithox.com/docs/troubleshooting#health-check-failed) explains each health check message. For how the switch between releases works with systemd and Caddy, see [zero-downtime deploys with systemd and Caddy](https://deploywithox.com/blog/zero-downtime-deploys-systemd-caddy), and if the app starts but lacks a value it needs, [systemd service environment variables](https://deploywithox.com/blog/systemd-service-environment-variables). ## Key takeaways - A Caddy 502 with "dial tcp ... connection refused" means Caddy reached no listener at the address it dialed. Check first whether anything listens there. - Make a new request and read its own line in the journal, with `journalctl -u caddy`. - Read where Caddy dials now from the admin API, and compare it with the file on disk and with `sudo ss -ltnp`. - Fix the side that is wrong, then run `caddy validate` and reload only if it passes. - `127.0.0.1`, `::1` and `localhost` are not the same. Write the same number on both sides, and keep the app on loopback. ## Frequently asked questions ### What does "dial tcp connect: connection refused" mean in Caddy? Caddy tried to connect to your app at the address in the message and was refused. Linux gives that error when no program listens at that address, and a firewall rule that rejects the connection can give it too. Check whether the app runs, and on which port and address, such as ::1 instead of 127.0.0.1, with sudo ss -ltnp. ### Why does caddy reload say connection refused on port 2019? caddy reload sends the new config to the admin API of a running Caddy, by default at localhost:2019. Connection refused there means no Caddy is listening on that admin address, so the reload had nowhere to go. On the test server it happened with the service stopped. Start the service, or pass the admin address the running Caddy really uses. ### Why do I get a 502 only for a few seconds during deploys? While the app restarts, nothing listens on its port, so each request in that gap gets a 502. Caddy's lb_try_duration option makes it retry the backend for a set time before giving up, which hides a short gap. Starting the new version beside the old one and switching after it answers avoids the gap. ## Next step On your own server, run [the checks](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-diagnose) against the site that gives the 502: a new request and its journal line, the running dial address, `sudo ss -ltnp`, and a direct `curl`. If you would rather have a tool pick the port and check it before any visitor arrives, the [quickstart](https://deploywithox.com/docs/quickstart) takes you from a fresh server to a live app. New to running apps on your own server? [How ox runs your apps](https://deploywithox.com/self-hosting) shows what ox sets up on your server. On this page - [What the 502 and "connection refused" tell you](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-meaning) - [Check a running server](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-diagnose) - [Fix the port and reload Caddy](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-fix) - [Reproduce it safely on your own machine](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-lab) - [Same port, wrong address: 127.0.0.1 and ::1](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-address) - [When the app is not running](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-down) - [Clean up the lab](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-cleanup) - [How ox keeps the port and the proxy in step](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-ox) - [Key takeaways](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#key-takeaways) - [Frequently asked questions](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#faq) - [What does "dial tcp connect: connection refused" mean in Caddy?](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-faq-meaning) - [Why does caddy reload say connection refused on port 2019?](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-faq-reload) - [Why do I get a 502 only for a few seconds during deploys?](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-faq-intermittent) - [Next step](https://deploywithox.com/blog/caddy-reverse-proxy-502-connection-refused#heading-next) Written by Saurav Sharma Builds ox. Writes about running apps on servers you own, without Docker and without a platform bill. [Older post systemd Service Environment Variables, and Why They Fail](https://deploywithox.com/blog/systemd-service-environment-variables) [Newer post Strapi Uploads Gone After Redeploy? Restore and Keep Them](https://deploywithox.com/blog/strapi-uploads-disappear-after-redeploy) ## Keep reading - Guides [PostgreSQL Connection String Password Special Characters](https://deploywithox.com/blog/postgres-connection-string-special-characters) Fix a PostgreSQL connection string whose password has special characters: percent-encode it once, check how libpq reads it, keep it off the command line. Saurav Sharma Dec 3, 2026 16 min read - Guides [Back Up a Live SQLite Database Before Deploy, and Prove It](https://deploywithox.com/blog/sqlite-backup-before-deploy) Back up a live SQLite database before a deploy with .backup or VACUUM INTO, not cp, which can miss WAL commits. Then prove the copy in an empty folder. Saurav Sharma Dec 2, 2026 15 min read - Guides [Strapi Uploads Gone After Redeploy? Restore and Keep Them](https://deploywithox.com/blog/strapi-uploads-disappear-after-redeploy) Recover Strapi uploads that return 404 after a redeploy. Compare files across releases, resolve conflicting copies, and keep uploads in one shared folder. Saurav Sharma Dec 2, 2026 22 min read ## Deploy to a server you own ox sets up systemd, Caddy and PostgreSQL on your Ubuntu server and deploys on every push. Free for your first project. [Sign up with GitHub](https://deploywithox.com/login/github) [Read the docs](https://deploywithox.com/docs)