Reverse proxy error

1. The problem I’m having:

I’m having trouble using the reverse_proxy setting to load another container service. Specifically, http://styx.twilightstar.net which should redirect to another container results in a 502 error.

I independently verified that the other container is working correctly by directly loading it via browser at http://127.0.0.1:9089

2. Error messages and/or full log output:

caddy  | {"level":"error","ts":1683417047.6620417,"logger":"http.log.error","msg":"dial tcp 127.0.0.1:9089: connect: connection refused","request":{"remote_ip":"99.150.202.198","remote_port":"49686","proto":"HTTP/3.0","method":"GET","host":"styx.twilightstar.net","uri":"/","headers":{"Dnt":["1"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-User":["?1"],"Cache-Control":["max-age=0"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Ch-Ua":["\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\""],"Sec-Ch-Ua-Mobile":["?0"],"Sec-Ch-Ua-Platform":["\"macOS\""],"Sec-Fetch-Site":["none"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Dest":["document"],"Accept-Language":["en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h3","server_name":"styx.twilightstar.net"}},"duration":0.000377565,"status":502,"err_id":"8h7arawjb","err_trace":"reverseproxy.statusError (reverseproxy.go:1299)"}
caddy  | {"level":"error","ts":1683417052.6336331,"logger":"http.log.error","msg":"dial tcp 127.0.0.1:9089: connect: connection refused","request":{"remote_ip":"195.74.76.198","remote_port":"48769","proto":"HTTP/2.0","method":"HEAD","host":"styx.twilightstar.net","uri":"/","headers":{"Referer":["http://styx.twilightstar.net"],"User-Agent":["Go-http-client/2.0"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"styx.twilightstar.net"}},"duration":0.000366725,"status":502,"err_id":"id06zemt4","err_trace":"reverseproxy.statusError (reverseproxy.go:1299)"}
caddy  | {"level":"error","ts":1683417271.3385246,"logger":"http.log.error","msg":"dial tcp 127.0.0.1:9089: connect: connection refused","request":{"remote_ip":"152.70.152.203","remote_port":"39822","proto":"HTTP/1.1","method":"GET","host":"styx.twilightstar.net","uri":"/","headers":{"Accept-Language":["en,*;q=0.1"],"Accept-Encoding":["gzip, deflate, br, zstd, bzip2, lzma, lzma2, lzip"],"Accept-Charset":["us-ascii,ISO-8859-1,ISO-8859-2,ISO-8859-3,ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15,ISO-8859-16,windows-1250,windows-1251,windows-1252,windows-1256,windows-1257,cp437,cp737,cp850,cp852,cp866,x-cp866-u,x-mac,x-mac-ce,x-kam-cs,koi8-r,koi8-u,koi8-ru,TCVN-5712,VISCII,utf-8"],"Connection":["keep-alive"],"User-Agent":["Links (2.25; Linux 5.15.0-1034-oracle x86_64; GNU C 11.2; text)"],"Accept":["*/*"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"","server_name":"styx.twilightstar.net"}},"duration":0.000337607,"status":502,"err_id":"fx64fxix3","err_trace":"reverseproxy.statusError (reverseproxy.go:1299)"}

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

Pulled from docker hub

a. System environment:

Ubuntu Linux/Docker

b. Command:

docker-compose -f ~/docker/caddy.yml up -d caddy

c. Service/unit/compose file:

 version: "3.7"

 services:
   caddy:
     container_name: caddy
     image: caddy:latest
     restart: unless-stopped
     ports:
       - "80:80"
       - "80:80/udp"
       - "443:443"
       - "443:443/udp"
     volumes:
       - $APPDIR/Caddyfile:/etc/caddy/Caddyfile

d. My complete Caddy config:

 styx.twilightstar.net {
     reverse_proxy http://127.0.0.1:9089
 }

5. Links to relevant resources:

127.0.0.1 aka localhost inside of a container means “this same container”. If you want to connect to another container, they need to share a Docker network, then you can use the container name as the address.

Also, please ensure you’re persisting /data as a volume, otherwise you risk losing your certs and keys when you recreate your Caddy container. And also Caddy never uses port 80 with UDP, only TCP, so you can remove that line. UDP is only used on port 443 for HTTP/3

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.