带有HTTP和WS协议的Apache代理



我在服务器上运行的服务,可以通过HTTP和WS访问它。当我要在Apache2中配置子域时,我会有困境,因为我希望两个协议都适用于同一子域。也就是说,如果传入连接使用HTTP协议(http://),则Apache2必须将连接重定向到SERVICE:HTTP_PORT,如果它是Websocket(ws://),我希望它能转到SERVICE:WS_PORT。有什么办法可以这样做,而无需使用/WS或/websocket等路由?

websockets和apache代理重复:如何配置mod_proxy_wstunnel?

我遵循此答案的说明:https://stackoverflow.com/a/35141086/4763630

这是我服务器的最终Apache2配置:

<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>

现在我可以使用http://https://ws://wss://

最新更新