I have a service running on my server, it is possible to access it through http and ws. I have the dilemma when I am going to configure the subdomain in Apache2, because I would like to have both protocols working for the same subdomain. That is, if the incoming connection is using the HTTP protocol (http://) then Apache2 must redirect the connection to SERVICE:HTTP_PORT, if it is websocket (ws://) I want it to go to SERVICE:WS_PORT. Is there any way to do this without having to use routes like / ws or / websocket to make the connection?
            Asked
            
        
        
            Active
            
        
            Viewed 835 times
        
    0
            
            
        
        Ander Acosta
        
- 1,060
 - 1
 - 12
 - 25
 
1 Answers
1
            Duplicate for WebSockets and Apache proxy : how to configure mod_proxy_wstunnel?
I followed the instructions of this answer: https://stackoverflow.com/a/35141086/4763630
This is the final Apache2 config for my server:
<VirtualHost *:80>
  ServerAdmin admin@example.com
  ServerName service.example.com
  RewriteEngine On
  RewriteCond %{HTTP:Upgrade}   =websocket [NC]
  RewriteRule /(.*)     wss://service.example.com/$1 [P,L]
  RewriteCond %{HTTP:Upgrade}   !=websocket [NC]
  RewriteRule /(.*)     https://service.example.com/$1 [P,L]
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
  ServerAdmin admin@example.com
  ServerName service.example.com
  RewriteEngine On
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteRule /(.*)           ws://localhost:1886/$1 [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)           http://localhost:1996/$1 [P,L]
  ProxyPassReverse /          https://service.example.com
  <Location "/">
    Header set Access-Control-Allow-Origin "*"
    Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
    Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
  </Location>
  SSLEngine On
  SSLProtocol All -SSLv2 -SSLv3
  SSLHonorCipherOrder On
  SSLCipherSuite HIGH:MEDIUM
  SSLCertificateFile cert.pem
  SSLCertificateKeyFile privkey.pem
  SSLCertificateChainFile chain.pem
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now I can access with http://, https://, ws:// and wss://.
        Ander Acosta
        
- 1,060
 - 1
 - 12
 - 25