Setting up WebSocket Proxies on OpenLiteSpeed

How Can We Help?
< Back
You are here:
Print

Set up WebSocket Proxies on OpenLiteSpeed

In this article we will explain how to set up a WebSocket proxy to a WebSocket backend. With a WebSocket proxy, OpenLiteSpeed will forward traffic with the WebSocket upgrade request to the WebSocket backend specified in the WebSocket proxy. Traffic without this upgrade request can be served via a separate Context. This means you can keep your WebSocket application behind OpenLiteSpeed, both for security and efficient handling of connections. It also means you can take advantage of OpenLiteSpeed’s efficient service of static or dynamic content while still having WebSocket content on the same page. For more information about WebSocket and WebSocket proxies, please see our blog.

We will not cover here how to set up a WebSocket backend. (The first step is to have a WebSocket backend set up.) WebSocket proxies are only available for OpenLiteSpeed version 1.2 and up.

Have a WebSocket Backend Ready

This is probably the hardest part. Before you start setting up the WebSocket proxy, make sure your WebSocket backend works properly.

There are many, many WebSocket backends to choose from and they should all work well with OpenLiteSpeed’s WebSocket proxy. For testing purposes we used the libwebsockets test server. In this guide, we will make references to files from the libwebsocket backend. libwebsockets comes with a handy WebSocket test page.

Add a WebSocket Proxy to Your Virtual Host

The WebSocket Proxy tab is inside your virtual host settings (WebAdmin console > Virtual Hosts > your virtual host > WebSocket Proxy > Add).

Enter the URI for your WebSocket application (relative to your virtual host’s domain) and the Address of the WebSocket backend.

The libwebsocket test page, “test.html”, has the WebSocket application URI set as ws://domain/, so I set my URI to /. By default, the libwebsocket server listens on 127.0.0.1:7681, so that’s my address value. You can configure your pages and backends to have the WebSocket application in a different URI and your backend at a different location.

Save the WebSocket proxy and graceful restart to apply changes.

Add the WebSocket Page File to the Virtual Host

Move the file for the page with your WebSocket applications to the location you’d like to have it.

I’m putting mine in the document root for the virtual host that I created in previous guides with OpenLiteSpeed, “Example3”

cp /usr/local/libwebsocket/test-server/test.html /usr/local/lsws/DEFAULT/html

Test It Out

Point your browser to 127.0.0.1:8088/test.html (or wherever you put the WebSocket page) and your WebSocket page should come up, functioning better than ever.