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:

  1. Test your backend's WebSocket endpoint from the Apache host.

    wscat comes with the node-ws package on Ubuntu and Debian and runs without sudo. The URL takes your backend's host, port, and path in place of app.internal.example:8081/ws/. -x hello sends a text message and -w 2 gives the echo server two seconds to return hello. 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
  2. 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.

  3. Enable the proxy and proxy_http modules 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.

  4. Confirm that Apache's configuration lists both proxy modules.
    $ sudo apache2ctl -M | grep -E 'proxy(_http)?_module'
     proxy_module (shared)
     proxy_http_module (shared)
  5. Open the virtual host's source file in your usual editor.

    The example filename proxy.example.net.conf stands 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'
  6. Set a single ProxyPass mapping for the WebSocket path inside the selected <VirtualHost> block, above any broader ProxyPass rule.

    The first /ws/ stands for the path clients will use. The http:// 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 as ProxyPass /. upgrade=websocket selects the upgrade protocol for this mapping.

        ProxyPass "/ws/" "http://app.internal.example:8081/ws/" upgrade=websocket
  7. Save the file.
  8. Close the editor.

    sudoedit writes the saved version back to the virtual host file when the editor closes.

  9. 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.

  10. Reload the apache2 service 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
  11. 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 uses wss://. For the echo server, hello confirms 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

Share this guide