Project

General

Profile

[Solved] websockets (mod_proxy)

Added by fpcarv about 3 years ago

Hi everyone!

I'm trying to use a websocket server (c++ Boost::beast) behind lighttpd, with success but only if I use the following configuration for the proxy:

$HTTP["url"] =~ "^/wsck" {
    proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "6000" ) ) )
    proxy.header = ( "upgrade" => "enable" )
}

The server is invoked in js by:

var ws = new WebSocket("ws://127.0.0.1/wsck");

(wsck is a phony dir, it doesn't exist)


However if I try to use:

$HTTP["referer"] =~ "^ws:" {
    proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "6000" ) ) )
    proxy.header = ( "upgrade" => "enable" )
}

corresponding javascript :

var ws = new WebSocket("ws://127.0.0.1");

the thing does not work .... why?

What am I doing wrong?

Can we use just the scheme part (ws: or wss:) to direct the websocket calls to its server without the phony url?

Many thanks for your help,

-Francisco


Replies (3)

RE: websockets (mod_proxy) - Added by gstrauss about 3 years ago

While your javascript contains "ws://...", that is not what is sent to the HTTP server.

Please open the developer console in your browser and review what is sent in the HTTP request headers.

Please note that Referer is not always sent by clients. Some clients can be configured to strip Referer from the HTTP request. Some middleware proxies strip Referer from the HTTP request.

RE: websockets (mod_proxy) - Added by fpcarv about 3 years ago

Thanks for the tips! Very helpful!
I could never imagine that the browser would rewrite the javascript call for "ws://..." ...

Following your advice and after looking at the headers sent by the client, I guess the phony dir can be dropped by trapping the start of the weksockets conversation:

$REQUEST_HEADER["Upgrade"] == "websocket" {
    proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "6000" ) ) )
    proxy.header = ( "upgrade" => "enable" )
}

No Referer ;-)

Then the "naked" url javascript works:

var ws = new WebSocket("ws://127.0.0.1");

Is this OK?

RE: websockets (mod_proxy) - Added by gstrauss about 3 years ago

I could never imagine that the browser would rewrite the javascript call for "ws://..." ...

Translation: "I never imagined my assumptions could be wrong, and I did not attempt to verify them by testing or troubleshooting."

Is this OK?

Does it work for you and your site?

    (1-3/3)