Project

General

Profile

[Solved] Lighttpd mod_proxy and websocket

Added by Anonymous about 6 years ago

I'm trying to use mod_proxy with lighttpd 1.4.32 to handle websocket connections from javascript clients and a nodejs websocket server.

Lighttpd open the 80 standard port and the internal nodejs websocket server is listening on 8081 port.

I have configured mod_proxy as follow:

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

From the client I'm trying to connect as follow:

<script language="javascript" type="text/javascript">
var connection = new WebSocket('ws://10.134.76.34/websocket', ['hint-protocol']);
// Log messages from the server
connection.onmessage = function (e) {
  console.log('Server: ' + e.data);  
};
</script>

First problem, the connection is not established, when I open the page from Google Chrome I receive this error :

WebSocket connection to 'ws://10.134.76.34/websocket' failed: Error during WebSocket handshake: 'Connection' header is missing

So I'have manually add these headers as follow :

$HTTP["url"] =~ "^/websocket" {
    setenv.add-request-header   = ( "Connection" => "Upgrade")
    setenv.add-response-header  = ( "Connection" => "Upgrade")
    proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "8081" ) ) )
    proxy.header = ( "upgrade" => "enable")
}

Now the connection seems to be established.

Second problem, when the websocket server send the first data packet, Google Chrome on client side returns this error :

WebSocket connection to 'ws://10.134.76.34/websocket' failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1

I' dont know why mod_proxy modify the packets, maybe I miss something.
Can someone help me to configure mod_proxy to forward websocket traffic to an internal server?
Thanks so much.


Replies (5)

RE: Lighttpd mod_proxy and websocket - Added by gstrauss about 6 years ago

maybe I miss something.

Yes: a few lighttpd releases. Latest lighttpd is lighttpd 1.4.49. Support for websockets with mod_proxy was added in lighttpd 1.4.46. Docs_ModProxy

What distro are you using that is still stuck with lighttpd 1.4.32, released over 5 years ago?

RE: [Solved] Lighttpd mod_proxy and websocket - Added by Anonymous about 6 years ago

Hi , first of all thanks for the support, I really appreciate.

I use an old version of Lighttpd because it is inside a linux embedded machine and each time I need to cross compile to update it.

lighttpd mod_proxy does not currently support SSL/TLS connections to the backend server.

My web server needs to listen also the 443 port but is not possible to connect to an insecure websocket if pages was loaded over https.
Is the SSL/TLS support in the roadmap?
Thank you again.

RE: [Solved] Lighttpd mod_proxy and websocket - Added by gstrauss about 6 years ago

I use an old version of Lighttpd because it is inside a linux embedded machine and each time I need to cross compile to update it.

Well, since you haven't updated in about 5 years, that is a pretty lame excuse.

Is the SSL/TLS support in the roadmap?

SSL/TLS to a backend via mod_proxy is currently a very low priority.

If you're on an embedded system, and your websocket server is on an embedded system, then you really should consider avoiding the overhead your desire to have an SSL/TLS connection from localhost to localhost.

lighttpd can listen on 443 and can proxy back to your websocket server. lighttpd mod_proxy can be configured to set the Forwarded header so that the backend proxy can be told that the upstream connection is secure.

lighttpd can also be the websocket tunnel endpoint with mod_wstunnel, and can pass the websocket data payload, e.g. RESTful requests, to a backend.

RE: [Solved] Lighttpd mod_proxy and websocket - Added by Anonymous about 6 years ago

lighttpd can listen on 443 and can proxy back to your websocket server. lighttpd mod_proxy can be configured to set the Forwarded header so that the backend proxy can be told that the upstream connection is secure.

That's great!
So I can connect from clients using wss schema and lighttpd proxy back to my local (unsecure) websocket.
Are there any examples of how to configure mod proxy this way?

RE: [Solved] Lighttpd mod_proxy and websocket - Added by gstrauss about 6 years ago

Are there any examples of how to configure mod proxy this way?

Yes. Have you tried reading the documentation? There is an example in the mod_proxy documentation. The example notes that you need lighttpd 1.4.46 or later.

    (1-5/5)