代理websocket wss://到ws:// apache

Sam*_*Rad 5 apache wss proxypass websocket

我搜索了很多,但我无法将我的websocket连接到wss://,我发现有一种代理方式wss://domain.com:9090和apache应用代理并将请求重定向到正常的ws:/ /domain.com:9090服务器正在运行

ProxyPass /websocket ws://domain.com:9090
ProxyPassReverse /websocket ws://domain.com:9090
Run Code Online (Sandbox Code Playgroud)

这个代码在Apache的配置会从任何地址发送请求与/ WebSocket的到WS结束://domain.com:9090例如:WS://的WebSocket将WS://domain.com:9090

我想为wss做这个://也是ex wss:// websocket必须指向ws://domain.com:9090

它dosnt工作,我在浏览器控制台中收到此错误:

failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Run Code Online (Sandbox Code Playgroud)

这里有什么错吗?谢谢 .

Sam*_*Rad 11

我工作24小时找到这个,并搜索了很多论坛,但没有人写成功.这是我的服务器配置:

CentOS版本6.7,Apache 4.2.18

这是我最后做的:首先我发现模块/ mod_proxy_wstunnel.so必须在apache配置文件中启用,但是我的apache没有那个模块,经过大量搜索我发现模块在apache 2.4.5中可用然后.

https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html

我下载了https://archive.apache.org/dist/httpd/httpd-2.4.18.tar.gz 解压后的httpd-2.4.18\modules\proxy\mod_proxy_wstunnel.c并上传到我的服务器根然后从终端可以编译再次与这些共同点:

chmod 755 mod_proxy_wstunnel.c #set permission
pxs -i -a -c mod_proxy_tunnel.c #compile module
Run Code Online (Sandbox Code Playgroud)

pxs命令确实编译了模块并在apache配置文件中写入以加载它

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
Run Code Online (Sandbox Code Playgroud)

之后我将这些行添加到apache配置文件的末尾:

RewriteEngine on
ProxyRequests Off
ProxyPreserveHost on
ProxyPass /myws ws://mysite.com:8091
ProxyPassReverse /myws ws://mysite.com:8091
Run Code Online (Sandbox Code Playgroud)

现在:它有效!在客户端js你可以像这样设置ws url:

var protocol = 'ws://'; 
if (window.location.protocol === 'https:') {
            protocol = 'wss://';
   }

 var wsUri =protocol+ "mysite.com/myws";  

 var ws = new WebSocket(wsUri);
Run Code Online (Sandbox Code Playgroud)

并且它会将请求转发给ws://mysite.com:8091对于加载了https或http的页面无关紧要,它会将所有以/ myws结尾的请求指向ws://mysite.com:8091


Flo*_*oel 11

您需要启用一些 Apache2 模块:

$ a2enmod proxy proxy_wstunnel proxy_http rewrite
Run Code Online (Sandbox Code Playgroud)

然后你可以使用这个配置来解决你的问题。

    ProxyRequests off
    ProxyVia on      
    RewriteEngine On

    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://example.com:9090/$1 [P,L]

    ProxyPass               /websocket http://example.com:9090/websocket
    ProxyPassReverse        /websocket http://example.com:9090/websocket
Run Code Online (Sandbox Code Playgroud)

Apache2 自动升级到带有 ws:// 的 websocket 连接,您不需要手动设置 ws://。我尝试了几十种配置,这是唯一对我有用的配置。


Jos*_*ias 5

我试图解决的问题与此类似。我有一个在 CentOs 7 上的 Apache 2.4 下运行的反向代理,它必须处理 https 和 wss 请求。

在反向代理后面,我的应用程序服务器在内部网络上运行。/etc/httpd/httpd.conf 配置文件中的虚拟主机配置如下:

<VirtualHost *:443>
   ServerName example.com
   RewriteCond %(HTTP:Upgrade) websocket [NC]   # Required to handle the websocket connection
   RewriteCond %(HTTP:Connection) upgrade [NC]
   RewriteRule /(.*) ws://192.160.0.1/$1 [P,L]

  SSLEngine on # SSL Certificates handling
  SSLCertificateFile ssl/cert.pem # Public Certificate
  SSLCertificateKeyFile ssl/key.pem # Private certificate
  SSLCertificateChainFile ssl/ca.pem # CA or chain certificate

 ProxyPreserveHost On
 ProxyPass /websocket ws://192.168.0.1 # First you need to write the specific rules
 ProxyPassReverse /websocket ws://102.168.0.1
 ProxyPass / http://192.168.0.1 # Then the generic rules for the proxy.
 ProxyPassReverse / http://192.168.0.1
 </VirtualHost>
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您必须替换 ServerName、SSL 证书位置和代理的目标。