Apache Config
Websockets Proxy
WSS Request
WS Backend
Network Configuration

Apache Config Websockets Proxy WSS request to WS backend

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

WebSockets is a powerful protocol providing full-duplex communication channels over a single long-lived TCP connection. It is extensively used in real-time data transfer scenarios such as chat applications, financial tickers, game servers, and more. Apache HTTP Server, being one of the most robust and widely used web servers, provides mechanisms through which WebSocket connections can be proxied to a WebSocket server that may only support the basic WebSocket protocol (ws://).

Configuring Apache to Proxy WebSocket requests

To set up Apache as a WebSocket proxy, particularly upgrading from WSS (WebSocket Secure Protocol, or WebSocket over TLS/SSL) to an unsecured WS backend, involves leveraging the mod_proxy_wstunnel module. This module is specifically designed to handle WebSocket upgrade requests and tunnel these through Apache to the backend WebSocket server.

Apache Module: mod_proxy_wstunnel

mod_proxy_wstunnel is an essential module in the Apache server allowing forwarding of WebSocket connections to another server. This module must be enabled to proxy WebSocket connections.

Steps to Enable and Configure

  1. Enable mod_proxy_wstunnel
    Depending on your Apache configuration and operating system, you generally enable modules using the a2enmod command or by editing your Apache configuration files directly.
bash
   sudo a2enmod proxy_wstunnel
  1. Configure your VirtualHost or server block Here is a basic configuration snippet for setting up a WebSocket proxy inside a VirtualHost block in Apache:
apache
1   <VirtualHost *:443>
2       ServerName example.com
3       SSLEngine on
4       SSLCertificateFile /path/to/cert.pem
5       SSLCertificateKeyFile /path/to/key.pem
6       
7       # Reverse proxy for WebSocket traffic
8       ProxyPass "/wsapp/"  "ws://backendserver.com:8080/"
9       ProxyPassReverse "/wsapp/"  "ws://backendserver.com:8080/"
10   </VirtualHost>

In the above configuration, all traffic to wss://example.com/wsapp/ will be proxied to ws://backendserver.com:8080/.

Considerations for SSL/TLS

The ProxyPass directive above handles the upgrade from HTTP/HTTPS to WebSocket. However, Apache itself does not need to handle WebSocket traffic decryption since it simply tunnels the encryption through to the backend. The backend WebSocket server will still see this as a typical ws:// request.

This can be critical, especially when the backend service is on an internal network with controlled security measures, which simplifies the SSL/TLS management by centralizing it on the Apache proxy.

Detailed Example

Below is a more detailed example which also includes logging specific to the WebSocket proxy:

apache
1<VirtualHost *:443>
2    ServerName example.com
3    SSLEngine on
4    SSLCertificateFile "/etc/ssl/certs/example.pem"
5    SSLCertificateKeyFile "/etc/ssl/private/example.key"
6
7    ErrorLog ${APACHE_LOG_DIR}/error.log
8    CustomLog ${APACHE_LOG_DIR}/access.log combined
9    
10    # WebSocket reverse proxy
11    ProxyPass "/ws/" "ws://192.168.1.50:3000/"
12    ProxyPassReverse "/ws/" "ws://192.168.1.50:3000/"
13    
14    LogLevel info
15    ProxyRequests Off
16    ProxyVia On
17
18    <Proxy *>
19        Require all granted
20    </Proxy>
21</VirtualHost>

In this configuration, detailed logs are configured that help in troubleshooting.

Summary Table

Here’s a table summarizing the key components discussed:

ComponentDescription
mod_proxy_wstunnelModule required for handling WebSocket connections in Apache.
wss:// to ws://Proxying from secured WebSocket connection to unsecured.
ProxyPassDirective to map a local URI to a backend WebSocket server.
ProxyPassReverseDirective to manage response headers for proxied requests.

Security and Performance

When proxying from WSS to WS, security considerations mainly involve ensuring that the manner in which Apache is exposed to the internet is secure. Apache serves as an SSL endpoint, thereby offloading this task from the backend. However, the internal traffic from Apache to the backend server is unencrypted (ws://), which might be acceptable within a secure, controlled network.

Performance-wise, using Apache as a WebSocket proxy should be tested under load conditions. While Apache is quite capable of handling numerous connections, the specific use-case, traffic load, and WebSocket message size should be monitored.

This setup allows leveraging Apache's robustness and feature-rich configurations, like authentication and logging, while using WebSocket services, providing a powerful setup for real-time web applications.


Course illustration
Course illustration

All Rights Reserved.