0

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?

1 Answer 1

2

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 [email protected]
  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 [email protected]
  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://.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.