lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
3.02k stars 173 forks source link

Http error 502 using replicas #293

Closed kidcuauh closed 3 years ago

kidcuauh commented 3 years ago

Reverse proxy works good avoiding replicas. It's a frontend Vuetify app docker service running under caddy:2.4.3-alpine The reverse proxy is running latest stable lucaslorentz/caddy-docker-proxy:2.3.5-alpine

vuetify.yaml with 2 replicas:

version: '3.7'
services:
  app:
    image: vuetify:master
    networks:
      - web
    deploy:
      labels:
        - caddy_0=test.domain.com
        - caddy_0.reverse_proxy="{{upstreams 80}}"
        - caddy_0.tls=cuau@domain.com
        - caddy_0.tls.on_demand=
      replicas: 2
      restart_policy:
        condition: any
networks:
  web:
    external:
      name: web

Log reverse-proxy:

2021/08/31 17:45:30 [INFO] Skipping default Caddyfile because no path is set
2021/08/31 17:45:30 [INFO] New Caddyfile:
test.domain.com {
        reverse_proxy "10.0.3.15:80 10.0.3.18:80"
        tls cuau@domain.com {
                on_demand
        }
}

Error log:

{"level":"error","ts":1630431969.4226747,"logger":"http.log.error","msg":"dial tcp: lookup 10.0.3.15:80 10.0.3.18:80: no such host","request":{"remote_addr":"10.0.0.2:2708","proto":"HTTP/2.0","method":"GET","host":"test.domain.com","uri":"/","headers":{"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.9"],"Sec-Fetch-User":["?1"],"Sec-Ch-Ua":["\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\""],"Sec-Ch-Ua-Mobile":["?0"],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.9"],"Cache-Control":["max-age=0"],"Sec-Fetch-Site":["none"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-Dest":["document"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","proto_mutual":true,"server_name":"test.domain.com"}},"duration":0.001490715,"status":502,"err_id":"r0rwi936a","err_trace":"reverseproxy.statusError (reverseproxy.go:852)"}
francislavoie commented 3 years ago

Please use markdown code formatting when posting issues. It's very hard to read your configs and logs, because whitespace wasn't preserved.

kidcuauh commented 3 years ago

I did a new test with curl to know if my reverse proxy container can communicate with one of the replicas (I tested both) and there is no problem, so there is something else that prevents the reverse proxy working properly.

/ # curl -Iv 8dc5681142ff
*   Trying 10.0.3.15:80...
* TCP_NODELAY set
* Connected to 8dc5681142ff (10.0.3.15) port 80 (#0)
> HEAD / HTTP/1.1
> Host: 8dc5681142ff
> User-Agent: curl/7.67.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Accept-Ranges: bytes
Accept-Ranges: bytes
< Content-Length: 9765
Content-Length: 9765
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Etag: "qynu9g7j9"
Etag: "qynu9g7j9"
< Last-Modified: Mon, 30 Aug 2021 16:10:28 GMT
Last-Modified: Mon, 30 Aug 2021 16:10:28 GMT
< Server: Caddy
Server: Caddy
< Date: Wed, 01 Sep 2021 04:10:06 GMT
Date: Wed, 01 Sep 2021 04:10:06 GMT
<
* Connection #0 to host 8dc5681142ff left intact

Am I missing something in the config?

francislavoie commented 3 years ago

Ah, right. The issue is your quotes here:

        - caddy_0.reverse_proxy="{{upstreams 80}}"

The quotes are being preserved in the Caddyfile, which makes Caddy try to connect to a single upstream literally called 10.0.3.15:80 10.0.3.18:80 which doesn't make sense.

See at the bottom of this section, the note about quotes: https://github.com/lucaslorentz/caddy-docker-proxy#upstreams

kidcuauh commented 3 years ago

Thanks! I confirm it works.

The note says quotes should only be used with yaml which I think I'm using. Also, following all examples in folder are with quotes since it's a yaml file: ​https://github.com/lucaslorentz/caddy-docker-proxy/blob/master/examples/standalone.yaml