Languages
[Edit]
EN

apache2 - redirect http:// and ws:// with proxy module

11 points
Created by:
Kate_C
2952

In this article we are going to look on problem how to redirect domain traffic that uses both websockets and http from one address to another with apache2. In network we can find a lot of examples that do not work because of redirection rules priority. This example was tested on Debian but can be used with other operating systems too.

To solve the problem it is necessary to execute following steps:

1. Enable proxy modules

To make redirection possible it is necessary to enable proxy modules at first.

Note: this approach was tested with apache 2.4.

Execute in terminal following commands to enable apache2 modules: 

a2enmod rewrite
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

2. Create ViarutaServer configuration

Create new or open one of the files located in /etc/apache2/sites-enabled/ directory and put following rule inside:

<VirtualHost *:80>
  ServerName mydomain.com

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

  ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Note: this configuration redirects all trafic from mydomain.com address (port 80) to localhost:8080 address including ws:// and http://.

3. Restart apache2 server

Run followin command to restart apache2 serwer:

systemctl restart apache2

Note: replace command with other if non Debian operating system is used.

Hey 👋
Would you like to know what we do?
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.