【问题标题】:Apache reverse proxy for websockets用于 websockets 的 Apache 反向代理
【发布时间】:2021-05-25 08:14:10
【问题描述】:

我在我的服务器上使用 Apache 代理端口 80 和 443 上的流量,以分隔运行不同网站和服务的 VM。我在为需要 websocket 的 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,同样的错误。

【问题讨论】:

标签: apache websocket reverse-proxy


【解决方案1】:

你可以试试:

Ubuntu

a2enmod proxy

a2enmod proxy_http

a2enmod proxy_wstunnel

Centos

  1. 打开代理的模块配置文件。 sudo vi /etc/httpd/conf.modules.d/00-proxy.conf
  2. 与代理相关的所有模块都列在此配置文件中。验证以下行是否存在且未注释。 LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_wstunnel modules/mod_proxy_wstunnel.so
  3. 如果您对文件进行了任何更改,请立即保存。
  4. 重新启动 Apache Web 服务器以应用您的更改。 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 的状态设置为 on 或 off。要支持 WebSocket,必须打开它。

RewriteCond ${HTTP:Upgrade} websocket [NC] 必须匹配的条件才能让 RewriteRule 处理请求。

RewriteCond ${HTTP:Connection} 升级 [NC] 对某事

RewriteRule . “wss:/ws-backend%{REQUEST_URI}” [P]* 重写所有传入请求以使用 wss 协议,并将目标主机名替换为后端服务的主机名。

文档来自:How to Reverse Proxy Websockets with Apache 2.4

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-02-16
  • 2011-08-09
  • 2013-02-18
  • 2016-02-19
  • 1970-01-01
  • 2017-02-18
  • 2015-06-18
  • 2020-11-24
相关资源
最近更新 更多