【问题标题】:Apache proxy with HTTP and WS protocols具有 HTTP 和 WS 协议的 Apache 代理
【发布时间】:2019-07-15 08:14:10
【问题描述】:
我的服务器上运行了一个服务,可以通过 http 和 ws 访问它。当我要在 Apache2 中配置子域时,我遇到了两难境地,因为我希望两种协议都适用于同一个子域。也就是说,如果传入的连接使用的是HTTP协议(http://)那么Apache2必须将连接重定向到SERVICE:HTTP_PORT,如果是websocket(ws://)我希望它转到SERVICE:WS_PORT。有什么方法可以做到这一点,而不必使用 /ws 或 /websocket 之类的路由来建立连接?
【问题讨论】:
标签:
http
websocket
mod-proxy
apache2.4
【解决方案1】:
复制WebSockets and Apache proxy : how to configure 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:// 访问。