Web socket proxy doesn't work as expected

1. The problem I’m having:

I’ve been running nginx on another machine but moved everything away, expect a phoenix web app. The proxy works unless I try to do something with the websocket

I try to convert the following nginx config:

upstream my_app {
    server 127.0.0.1:4000;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

 location / {
    try_files $uri @proxy;
  }

  location @proxy {
    include proxy_params;
    proxy_redirect off;
    proxy_pass http://my_app;
    # The following two headers need to be set in order
    # to keep the websocket connection open. Otherwise you'll see
    # HTTP 400's being returned from websocket connections.
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  }

  location /live {
    proxy_pass http://my_app;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Origin '';
  }

Did anyone encounter the same problem or know how to solve this?

My current config looks like this

demo.chocolatecoding.com {
        @websockets {
                header_regexp Connection Upgrade
                header Upgrade websocket
        }

        handle /live* {
                header X-Real-IP {remote_host}
                header X-Forwarded-For {proxy_add_x_forwarded_for}
                header Host {http_host}
                header Upgrade {http_upgrade}
                header Connection {upgrade}
                reverse_proxy @websockets casamento:4000
        }

        reverse_proxy casamento:4000
}

2. Error messages and/or full log output:

caddy  | {"level":"debug","ts":1680560986.132457,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"casamento:4000","duration":0.001130571,"request":{"remote_ip":"2001:678:aac:200::1","remote_port":"40664","proto":"HTTP/1.1","method":"GET","host":"demo.chocolatecoding.com","uri":"/live/websocket?_csrf_token=AxMAaGsKOhJ_JntXeUUSe1EiCR4CdTIknUvZ1sqFFlM97hpJ2WpyT3yt&_track_static%5B0%5D=https%3A%2F%2Fdemo.chocolatecoding.com%2Fassets%2Fapp-68adc0614a4d55f8cf4a90bfa2517b01.css%3Fvsn%3Dd&_track_static%5B1%5D=https%3A%2F%2Fdemo.chocolatecoding.com%2Fassets%2Fapp-b3cb35211d10aa0c5fdd7e45ca66e397.js%3Fvsn%3Dd&_mounts=0&vsn=2.0.0","headers":{"Cookie":[],"Sec-Websocket-Extensions":["permessage-deflate; client_max_window_bits"],"X-Forwarded-For":["2001:678:aac:200::1"],"X-Forwarded-Proto":["https"],"Sec-Websocket-Version":["13"],"Accept-Encoding":["gzip, deflate, br"],"Connection":["Upgrade"],"Upgrade":["websocket"],"Sec-Websocket-Key":["v14cSj8Zv2O5VEihI32RZA=="],"Accept-Language":["en-US,en;q=0.9"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"],"Pragma":["no-cache"],"X-Forwarded-Host":["demo.chocolatecoding.com"],"Origin":["https://demo.chocolatecoding.com"],"Cache-Control":["no-cache"]},"tls":{"resumed":true,"version":772,"cipher_suite":4865,"proto":"http/1.1","server_name":"demo.chocolatecoding.com"}},"headers":{"Date":["Mon, 03 Apr 2023 22:29:46 GMT"],"Server":["Cowboy"],"Cache-Control":["max-age=0, private, must-revalidate"],"Content-Length":["0"]},"status":403}

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

Docker via docker compose

a. System environment:

docker

Remove all this. It doesn’t do anything useful.

Caddy proxies WebSockets automatically, there’s nothing special you need to do with headers.

It should be sufficient to just do reverse_proxy casamento:4000

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