Skip to main content

Command Palette

Search for a command to run...

Configuring Apache as a Reverse Proxy with WebSockets for Seamless Communication

Updated
2 min read
Configuring Apache as a Reverse Proxy with WebSockets for Seamless Communication
G

A company founded back in 2019 by two visionary Engineers to create and provide you with the best results possible. We are a web developing company with great vision and plans for the future to be a part of others success. We understand that you have deadlines and reputation to keep... so we will meet ours! You can rely on Ghanshyam Digital To handle your job expertly every time.

In the process of setting up an efficient communication infrastructure, I encountered a challenge that demanded rigorous troubleshooting. After dedicated efforts, I achieved a successful configuration that ensured smooth operations.

The technological framework I employed consisted of a .NET Core application, orchestrated within a Docker container, serving as the API. To establish real-time socket connections, I employed SignalR. The entire ecosystem was orchestrated through the utilization of Apache as a reverse proxy. Initially, the system functioned optimally in the absence of a reverse proxy and Secure Sockets Layer (SSL) encryption.

Prerequisite

Before delving into the intricate setup, it's imperative to ensure that the mod_proxy module is enabled within Apache. Facilitating this entails executing a sequence of commands:

sudo a2enmod proxy_http
sudo service apache2 restart

Virtual Host Config

Upon securing the prerequisites, the journey involves crafting a comprehensive VirtualHost configuration. The intention is to adeptly channel both standard HTTP and WebSocket requests to their respective destinations. The following VirtualHost configuration exemplifies this approach:

<IfModule mod_ssl.c>
<VirtualHost *:443>
  RewriteEngine On
  ProxyPreserveHost On
  ProxyRequests Off

  # Facilitating WebSocket upgrades
  RewriteEngine On
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteRule /(.*)           ws://localhost:5000/$1 [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)           http://localhost:5000/$1 [P,L]


  ProxyPass "/" "http://localhost:5000/"
  ProxyPassReverse "/" "http://localhost:5000/"

  # This is the proxy setup for websockets.
  ProxyPass "/chatHub" "ws://localhost:5000/chatHub"
  ProxyPassReverse "/chatHub" "ws://localhost:5000/chatHub"

  ServerName site.com

  SSLCertificateFile /etc/letsencrypt/live/site.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/site.com/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

A pivotal aspect of this configuration involves the effective routing of the site.com/chatHub route. Through adept manipulation, this specific route is seamlessly forwarded to the WebSocket connection, ensuring that real-time communication is facilitated without any hindrances. This strategic routing optimization contributes to the overall efficiency and responsiveness of the communication ecosystem, enhancing the user experience and system performance.

By meticulously adhering to these instructions, one establishes a robust architecture where Apache seamlessly functions as a reverse proxy. This enables the efficient handling of both conventional HTTP and WebSocket traffic. The utilization of SSL certificates further fortifies the security aspect of this intricate communication ecosystem.

More from this blog

G

Ghanshyam Digital

33 posts

A Software Company delivering Softwares, Web Applications, Mobile Applications using latest technologies.