How to proxy websocket and http over lighttpd to different ports?

433 Views Asked by At

I have 2 separate applications. One is based on libwebsockts which works on port 8081 and the other based on libmicrohttpd which runs on port 8080. Both of these services run on localhost.

What I am trying to do is to use lighttpd with mod_proxy to proxy incoming requests to appropriate service. Which I failed to do.

      ---> wss://localhost/websocket ---            -wss-> 8081 port 
     /                                  \          /
react                                     lighttpd 
     \                                  /          \ 
      ---> http://localhost/app -------             -http-> 8080 port

It either work one way or the other.

My best guess of lighttpd.conf is

$HTTP["url"] =~ "^/app/" {
    proxy.server = (
        "" => ((
            "host" => "127.0.0.1",
            "port" => 8080
        )),
    )
}

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

$REQUEST_HEADER["Upgrade"] == "websocket" {
    setenv.set-request-header = ("Connection" => "Upgrade")
    proxy.header = ( "upgrade" => "enable" )
    proxy.server = ( "" => ( ( "host" => "localhost", "port" => "8081" ) ) )
}

What am I doing wrong?

1

There are 1 best solutions below

2
gstrauss On

You should add proxy.header = ( "upgrade" => "enable" ) to the block for /websocket/, and get rid of $REQUEST_HEADER["Upgrade"] == "websocket" { ... } You should never manually setenv.set-request-header = ("Connection" => "Upgrade") since the request either already provided that, or it did not. Also, if the request is HTTP/2, then the websocket request was made differently using HTTP/2 extended connect.

$HTTP["url"] =~ "^/app/" {
    proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )) )
}

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

Please see lighttpd mod_proxy documentation for additional options configurating lighttpd mod_proxy.