Live dashboards and chat apps keep a connection open so the server can push updates as they happen. On Ubuntu and Debian, Apache can proxy WebSocket traffic from a site's virtual host to the application that handles those connections.
A WebSocket starts with an HTTP request to upgrade the connection. Once the backend accepts the upgrade, Apache tunnels messages in both directions over that connection.
The backend needs a running WebSocket endpoint that the Apache host can reach. An HTTPS site uses wss:// for the client connection, even when Apache connects to the backend over plain HTTP on a trusted private network.
Steps to proxy WebSocket traffic with Apache:
- Test your backend's WebSocket endpoint from the Apache host.
wscatcomes with thenode-wspackage on Ubuntu and Debian and runs withoutsudo. The URL takes your backend's host, port, and path in place ofapp.internal.example:8081/ws/.-x hellosends a text message and-w 2gives the echo server two seconds to returnhello. For another application, use a known read-only message with an expected reply.$ wscat -c ws://app.internal.example:8081/ws/ -x hello -w 2 hello
- Locate the enabled virtual host for your Apache hostname and port.
$ sudo apache2ctl -S
The listing shows the hostname, listener port, and configuration file for each virtual host. On a site that serves HTTPS, choose the TLS virtual host.
- Enable the
proxyandproxy_httpmodules on the Apache host.$ sudo a2enmod proxy proxy_http
Apache 2.4.47 and later can handle the upgrade with
mod_proxy_http. For Apache 2.4.46 and earlier, see the legacy mod_proxy_wstunnel configuration. - Confirm that Apache's configuration lists both proxy modules.
$ sudo apache2ctl -M | grep -E 'proxy(_http)?_module' proxy_module (shared) proxy_http_module (shared)
- Open the virtual host's source file in your usual editor.
The example filename
proxy.example.net.confstands for your virtual host's file. In a standard installation, the entry in/etc/apache2/sites-enabled/links to the editable file in/etc/apache2/sites-available/.$ sudoedit '/etc/apache2/sites-available/proxy.example.net.conf'
- Set a single
ProxyPassmapping for the WebSocket path inside the selected<VirtualHost>block, above any broaderProxyPassrule.The first
/ws/stands for the path clients will use. Thehttp://URL takes the private host, port, and path from the direct test. A trailing slash on either side needs a trailing slash on the other. Apache uses the first matching rule. If a mapping already exists for that path, remove the old line and place the new one above any broader rule, such asProxyPass /.upgrade=websocketselects the upgrade protocol for this mapping.ProxyPass "/ws/" "http://app.internal.example:8081/ws/" upgrade=websocket
- Save the file.
- Close the editor.
sudoeditwrites the saved version back to the virtual host file when the editor closes. - Check the Apache configuration syntax.
$ sudo apache2ctl configtest Syntax OK
If the check reports an error, correct the file and rerun the check before reloading.
- Reload the
apache2service after the syntax check passes.The reload activates the modules and all pending Apache changes and makes the mapped backend path reachable by clients of this virtual host. Forward proxying should stay disabled (
ProxyRequests Off, the default). The application must enforce any required authentication and origin checks. Credentials and sensitive messages require HTTPS. To roll back the mapping, undo the edit, check the syntax again, and reload.$ sudo service apache2 reload
- Verify the message exchange through Apache with the same safe message.
From a client that can reach the site, the URL uses your Apache hostname, any nondefault port, and the public path in place of
proxy.example.net/ws/. An HTTPS site useswss://. For the echo server,helloconfirms the upgrade and the reply through Apache.$ wscat -c ws://proxy.example.net/ws/ -x hello -w 2 hello
For an HTTPS endpoint your browser can reach, a browser test can also check whether the application accepts the browser's origin.
Tool: WebSocket Connection Tester