简体   繁体   English

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

[英]Proxy websocket wss:// to ws:// apache

i searched alot but i couldnt connect my websocket to wss:// , i found that there is a way to proxy wss://domain.com:9090 and apache apply the proxy on it and redirect request to where the normal ws://domain.com:9090 server is running我搜索了很多,但我无法将我的 websocket 连接到 wss:// ,我发现有一种方法可以代理 wss://domain.com:9090 并且 apache 在其上应用代理并将请求重定向到正常 ws:/ /domain.com:9090 服务器正在运行

ProxyPass /websocket ws://domain.com:9090
ProxyPassReverse /websocket ws://domain.com:9090

this code in apache config will send request from any address ended with /websocket to ws://domain.com:9090 ex : ws://websocket will be ws://domain.com:9090 apache 配置中的此代码将从以 /websocket 结尾的任何地址发送请求到 ws://domain.com:9090 例如:ws://websocket 将是 ws://domain.com:9090

i want to do it for wss:// also ex wss://websocket must point to ws://domain.com:9090我想为 wss:// 也这样做 wss://websocket 必须指向 ws://domain.com:9090

it dosnt work and i get this error in browser console :它不起作用,我在浏览器控制台中收到此错误:

failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

is there any mistake here ?这里有什么错误吗? thanks you .谢谢你。

i worked 24 hours for find this and searched a lot of forum but no one write about success.我工作了 24 小时才找到这个并搜索了很多论坛,但没有人写关于成功的文章。 here is my server configuration :这是我的服务器配置:

CentOS release 6.7 , Apache 4.2.18 CentOS 6.7 版,Apache 4.2.18

here is what i did finally : first i found that modules/mod_proxy_wstunnel.so must be enable in apache config file , but my apache didn't have that module and after a lot of search i found that module is Available in apache 2.4.5 and later.这是我最后所做的:首先我发现必须在 apache 配置文件中启用 modules/mod_proxy_wstunnel.so,但是我的 apache 没有那个模块,经过大量搜索我发现该模块在 apache 2.4.5 中可用后来。

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

i downloaded https://archive.apache.org/dist/httpd/httpd-2.4.18.tar.gz extracted httpd-2.4.18\\modules\\proxy\\mod_proxy_wstunnel.c and uploaded to my server root then from terminal could compile it again with these commonds :我下载了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

pxs command did compile the module and wrote in apache config file to load it pxs 命令确实编译了模块并写入了 apache 配置文件以加载它

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

after that i added these lines to end of apache config file :之后,我将这些行添加到 apache 配置文件的末尾:

RewriteEngine on
ProxyRequests Off
ProxyPreserveHost on
ProxyPass /myws ws://mysite.com:8091
ProxyPassReverse /myws ws://mysite.com:8091

AND NOW : it works !现在:它起作用了! in client side js you can set ws url like this :在客户端 js 中,您可以像这样设置 ws url:

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

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

 var ws = new WebSocket(wsUri);

and it will forward request to ws://mysite.com:8091 doesnt matter the page loaded with https or http , it will direct all request ended with /myws to ws://mysite.com:8091它会将请求转发到 ws://mysite.com:8091 与加载 https 或 http 的页面无关,它将所有以 /myws 结尾的请求定向到 ws://mysite.com:8091

You need to enable some Apache2 modules:您需要启用一些 Apache2 模块:

$ a2enmod proxy proxy_wstunnel proxy_http rewrite

Then you can use this configuration to solve your problem.然后你可以使用这个配置来解决你的问题。

    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

Apache2 automatically upgrades the connection to websocket with ws://, you don't need to set the ws:// manually. Apache2 自动升级到带有 ws:// 的 websocket 连接,您不需要手动设置 ws://。 I tried dozens of configurations and this is the only one that worked for me.我尝试了几十种配置,这是唯一对我有用的配置。

wss 需要在 apache conf LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so 的 httpd.conf 中的模块取消注释行

the problem I was trying to solve was similar to this one.我试图解决的问题与此类似。 I have a reverse proxy running under Apache 2.4 on CentOs 7 which has to work with both https and wss requests.我有一个在 CentOs 7 上的 Apache 2.4 下运行的反向代理,它必须同时处理 https 和 wss 请求。

Behind the reverse proxy I have my app server running on an internal network.在反向代理的背后,我的应用服务器运行在内部网络上。 the virtual host configuration in the /etc/httpd/httpd.conf config file is as follows: /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>

In your case, you will have to replace the ServerName, the SSL certificates location, and the destination of the proxy.在您的情况下,您必须替换 ServerName、SSL 证书位置和代理的目的地。

The /websocket path is missing in your ProxyPass configuration path.您的 ProxyPass 配置路径中缺少 /websocket 路径。

Use:使用:

ProxyPass /websocket ws://example.com:9090/websocket
ProxyPassReverse /websocket ws://example.com:9090/websocket

Additional information: Like others mentioned, you have to uncomment the line:附加信息:与其他人提到的一样,您必须取消注释该行:

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

If you are also using a http ProxyPass thats relative path is "/" (forwarding everything directly), the specific "/websocket" path configuration must be configured first, otherwise "/" grabs first.如果您还使用相对路径为“/”(直接转发所有内容)的http ProxyPass,则必须先配置特定的“/websocket”路径配置,否则“/”会先抓取。

ProxyPass /websocket ws://example.com:9090/websocket
ProxyPassReverse /websocket ws://example.com:9090/websocket

ProxyPass balancer://ac-cluster/
ProxyPassReverse / http://example.com:9090

I did it for aria 2. I just enabled some modules and added a single line to config.我是为 aria 2 做的。我只是启用了一些模块并在配置中添加了一行。 (env: debian buster/apache 2.4). (环境:debian buster/apache 2.4)。 enabling modes:启用模式:

sudo a2enmod proxy proxy_balancer proxy_wstunnel proxy_http

and add this line to ssl site config file inside the virtual host directive :并将这一行添加到虚拟主机指令内的 ssl 站点配置文件:

ProxyPass /jsonrpc ws://127.0.0.1:6888/jsonrpc

I want to share this in case it helps somebody else avoid days of wasted time and effort.我想分享这个,以防它帮助其他人避免浪费时间和精力。

I was giving up after researching everything.在研究了一切之后我放弃了。 I was ready to start following the code of the different proxy modules, yes I know, a spiderweb..., but I was desperate.我准备开始遵循不同代理模块的代码,是的,我知道,蜘蛛网......,但我很绝望。 As a last resource I installed wireshark to follow exactly what was going on in my network.作为最后一个资源,我安装了wireshark 以准确跟踪我的网络中发生的情况。 After installing wireshark, the instructions asked me to restart my server through a power off/on cycle.安装wireshark后,说明要求我通过断电/开机循环重新启动我的服务器。 So, I did.所以,我做到了。 When I started tracing it, to my complete surprise, the server was proxying perfectly the wss requests to ws, no problems!当我开始跟踪它时,令我完全惊讶的是,服务器完美地将 wss 请求代理到 ws,没有问题! So I had the correct setup to start with but something got messed up in Ubuntu 20.4 / Apache 2.4.41 / node 14.17.2 that required a complete restart of the machine where the server operates.所以我有正确的设置开始,但是在 Ubuntu 20.4 / Apache 2.4.41 / node 14.17.2 中出现了一些问题,需要完全重新启动服务器运行的机器。 Crazy!疯了! But that was it...但就是这样...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM