Help:Set Up WebSocket Proxy

From OpenLiteSpeed
Jump to: navigation, search

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 > Configuration > Virtual Hosts > your virtual host > WebSocket Proxy > Add).

WebSocket Proxy Add.png

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.

WebSocket Proxy Settings.png

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 default virtual host that comes with OpenLiteSpeed, "Example."

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.

WebSocket Test.png