如何解决用于 websockets 的 Apache 反向代理
我在我的服务器上使用 Apache 代理端口 80 和 443 上的流量,以分离运行不同网站和服务的 VM。我在为需要 websockets 的 MeshCentral 设置代理时遇到问题。我使用 Debian 10 和 Apache 2.4.38。
我可以加载 MeshCentral,但是一旦我登录它就会尝试使用 websockets 并且我收到以下错误;
Firefox can’t establish a connection to the server at wss://example.com/control.ashx?auth=Uu7PBFNsswzzWoQaVNPH2N3ZwkWbx7DSsljaaY8cxthO5fcPVSz@sqLbGzyOpvxTxvfmV7WgwLdRklqLNYC5KQTjrZPCYDcNDvJ0AY7V8DGdUk68jK3sPfnc$Sl7rvhaQwR1xBukiZ8=. meshcentral.js:27:21
我已经添加了 wstunnel 代理
a2enmod proxy_wstunnel
并设置工作正常的 HTTP 和 HTTPS 代理
/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost On
ProxyPass "/" "http://192.168.200.11/"
ProxyPassReverse "/" "http://example.com/"
</VirtualHost>
/etc/apache2/sites-enabled/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
RewriteEngine on
RewriteCond ${HTTP:UPGRADE} websocket [NC]
RewriteCond ${HTTP:CONNECTION} upgrade [NC]
RewriteRule /(.*) "wss://example.com/$1" [P]
ProxyPreserveHost On
ProxyPass "/" "https://192.168.200.11/"
ProxyPassReverse "/" "https://example.com/"
SSLProxyEngine On
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
</VirtualHost>
</IfModule>
在尝试在 firefox 中加载页面之前,我已经重新启动了 apache,还尝试了 google-chrome,同样的错误。
解决方法
你可以试试:
Ubuntu
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel
Centos
- 打开代理的模块配置文件。 须藤vi /etc/httpd/conf.modules.d/00-proxy.conf
- 此配置文件中列出了与代理相关的所有模块。验证以下行是否存在且未注释。 LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_wstunnel modules/mod_proxy_wstunnel.so
- 如果您对文件进行了任何更改,请立即保存。
- 重新启动 Apache Web Server 以应用您的更改。 sudo systemctl restart httpd
配置:
<VirtualHost *:443>
ServerName ws.serverlab.ca
RewriteEngine on
RewriteCond ${HTTP:Upgrade} websocket [NC]
RewriteCond ${HTTP:Connection} upgrade [NC]
RewriteRule .* "wss:/localhost:3000/$1" [P,L]
<Proxy balancer://backend-cluster>
BalancerMember http://server01:3000
BalancerMember http://server02:3000
BalancerMember http://server03:3000
</Proxy>
ProxyPass / balancer://backend-cluster/
ProxyPassReverse / balancer://backend-cluster/
ProxyRequests off
</VirtualHost>
服务器名称 ws.serverlab.ca 将处理 WebSocket 连接的虚拟 Web 主机的主机名。
重写引擎开启 用于将 RewriteEngine 的状态设置为打开或关闭。要支持 WebSockets,必须打开它。
RewriteCond ${HTTP:Upgrade} websocket [NC] 必须匹配的条件才能让 RewriteRule 处理请求。
RewriteCond ${HTTP:Connection} 升级 [NC] 对某事
RewriteRule . “wss:/ws-backend%{REQUEST_URI}” [P]* 重写所有传入请求以使用 wss 协议,并将目标主机名替换为后端服务的主机名。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。