0

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 Answers1

1

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.

gstrauss
  • 2,091
  • 1
  • 12
  • 16
  • Thanks, for your answer. The whole issue was actually caused by wrong URL. At development setup I used `ws://localhost:8081/websocket/` without lighttpd and it worked fine. I wanted to keep same URL per development and production, and was counting on lighttpd to direct the traffict to correct service based on `/websocket/`. The approach that worked for me is to have a static IP in production and have different URLs. For production ws://192.168.0.1:8081/websocket/. I guess I'm gonna have to use some kind of Environment to swap it in my build system. Maybe you have any advice on this topic? – BlameCapitalism Jun 09 '23 at 16:04
  • There are more than a few ways to create a dev environment that mimics prod. One way is split DNS. On your **dev** machine, have prod-site.example.com point to 127.0.0.1 and run the dev server on localhost. Then, you can test on your local dev machine with a browser and prod-site.example.com will be your local site. – gstrauss Aug 03 '23 at 08:33