Weboscket 404 | Firefox can not connect to the server wss://

Hello!

I’m try use caddy as webserver with this config:

www.{$DOMAIN_NAME} {
    redir https://stagingserver.xyz
}

{$DOMAIN_NAME} {
    proxy / django:5000 {
        header_upstream Host {host}
        header_upstream X-Real-IP {remote}
        header_upstream X-Forwarded-Proto {scheme}
    }
    websocket /chat
    log stdout
    errors stdout
    gzip
}

But when I go to my site, I get a websocket connection error.

Firefox can not connect to the server wss://stagingserver.xyz/chat/2.

I once solved a similar error using nginx, but I do not remember how.
Now I have the same error when using Caddy

django_1        | 172.19.0.7:45526 - - [02/Oct/2017:18:29:32] "GET /chats/team_chat/2/" 200 4392
caddy_1         | 5.152.1.201 - - [02/Oct/2017:18:29:32 +0000] "GET /chats/team_chat/2/ HTTP/2.0" 200 1677
caddy_1         | 2017/10/02 18:29:33 [INFO] %d0%bd%d0%b0%d1%81%d1%82%d1%83%d0%bf%d0%b0%d0%b9.%d1%80%d1%84 - No such site at :80 (Remote: 176.121.14.98, Referer: )
django_1        | 172.19.0.7:45526 - - [02/Oct/2017:18:29:35] "GET /chat/2" 404 3383
caddy_1         | 5.152.1.201 - - [02/Oct/2017:18:29:35 +0000] "GET /chat/2 HTTP/1.1" 404 1342

This is my js code:

var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
socket = new WebSocket(ws_scheme + "://" + window.location.host + "/chat/" + room);

...

if (socket.readyState == WebSocket.OPEN) socket.onopen();

It works fine in the development environment.

How to solve this problem ? Thank you in advance!

Hi @narnikgamarnikus, welcome to the Caddy community.

Looking at this line in particular:

    websocket /chat

The docs specify the following format, with the addendum that an omitted path is treated as /, matching all requests:

websocket [path] command

To me, your usage implies that all requests should be proxied to a command located at /chat on the host filesystem.

However, you’ve configured a proxy that also matches all requests, and proxy beats websocket in order of execution, so I’d guess that the websocket middleware is never reached. Every request is sent to the proxy instead.

Does this sound like your intended configuration?

As for the proxy itself, I’d suggest using the websocket preset and see if that has a good effect (you can simplify the upstream headers too):

    proxy / django:5000 {
        transparent
        websocket
    }

https://caddyserver.com/docs/websocket
https://caddyserver.com/docs/proxy#presets

2 Likes

Thanks for the answer!

I read documentation, and I saw websocket [path] command, but I still did not understand what commands are being discussed, and where to look at the list of these command.

command is any command line application that takes input from stdin and outputs to stdout.

To use the example from the documentation:

websocket /echo cat

cat is a unix utility that reads input and sends it to output. As the docs say, this creates a simple echo server - Caddy takes a client’s request for /echo, feeds the message body through stdin to cat, and cat writes it straight back to stdout, which Caddy takes and returns to the client. A round trip in which the client receives exactly what they sent!

Take this alternate example:

websocket /blah "echo blah"

The command echo blah ignores stdin; it will do nothing with the message body, and instead write “blah” to stdout, so any clients requesting /blah from Caddy will receive “blah” in return, regardless of what they sent in their message body.

This should work with any command available to the host system - bash -c /path/to/script should work, I imagine, along with just about anything else, really - as long as it reads from stdin and writes to stdout.

1 Like