使用 HTTP 和 WS 协议的 Apache 代理
Apache proxy with HTTP and WS protocols
我的服务器上有一个服务运行,可以通过http和ws访问它。当我要在 Apache2 中配置子域时,我遇到了两难选择,因为我想让这两种协议都为同一个子域工作。也就是说,如果传入连接使用的是 HTTP 协议(http://
),那么 Apache2 必须将连接重定向到 SERVICE:HTTP_PORT
,如果是 websocket(ws://
),我希望它转到 SERVICE:WS_PORT
。有什么方法可以做到这一点而不必使用 /ws 或 /websocket 之类的路由来建立连接?
WebSockets and Apache proxy : how to configure mod_proxy_wstunnel?
重复
我遵循了这个答案的说明:
这是我的服务器的最终 Apache2 配置:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName service.example.com
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) wss://service.example.com/ [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) https://service.example.com/ [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/ [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://localhost:1996/ [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://
访问。
我的服务器上有一个服务运行,可以通过http和ws访问它。当我要在 Apache2 中配置子域时,我遇到了两难选择,因为我想让这两种协议都为同一个子域工作。也就是说,如果传入连接使用的是 HTTP 协议(http://
),那么 Apache2 必须将连接重定向到 SERVICE:HTTP_PORT
,如果是 websocket(ws://
),我希望它转到 SERVICE:WS_PORT
。有什么方法可以做到这一点而不必使用 /ws 或 /websocket 之类的路由来建立连接?
WebSockets and Apache proxy : how to configure mod_proxy_wstunnel?
重复我遵循了这个答案的说明:
这是我的服务器的最终 Apache2 配置:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName service.example.com
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) wss://service.example.com/ [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) https://service.example.com/ [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/ [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://localhost:1996/ [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://
访问。