1. The problem I’m having:
I want to use caddy reverse proxy to websocket server, but it’s failed.
my Caddyfile
is
mysite.com:443 {
tls s@gmail.com
encode gzip
route /mypathWiWL/ {
reverse_proxy 127.0.0.1:12456
}
handle {
reverse_proxy https://www.sohu.com {
trusted_proxies 0.0.0.0/0
header_up Host {upstream_hostport}
}
}
}
my request header likes:
GET /mypathWiWL/ HTTP/1.1\r\n
Host: 7.4.3.2\r\n
Connection: Upgrade\r\n
Upgrade: websocket\r\n
Sec-WebSocket-Version: 13\r\n
Sec-WebSocket-Key: dItNpIgo+/PdumK0EPyNxg==\r\n
udp-tunnel: true\r\n
\r\n
Under normal circumstances, caddy
will rewrite the Host field and forward this request header to the websocket server, and forward the response data returned from the server to the client.
but caddy
did not try to connect to the websocket
server at all, and directly returned 200 OK
. This is not what I expected.
with nginx
, the following settings works fine:
location /mypathWiWL/ {
proxy_redirect off;
proxy_pass http://127.0.0.1:12456;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
}
2. Error messages and/or full log output:
the return info is
HTTP/1.1 200 OK\r\n
Alt-Svc: h3=\":443\"; ma=2592000\r\n
Server: Caddy\r\n
Date: Wed, 25 Sep 2024 00:37:30 GMT\r\n
Content-Length: 0\r\n
\r\n
3. Caddy version:
v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=
4. How I installed and ran Caddy:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
caddy run --config /etc/caddy/Caddyfile
a. System environment:
Linux main 5.15.0-122-generic #132-Ubuntu SMP Thu Aug 29 13:45:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux