将 wss://sub.domain.com 转发到 ws://127.0.0.1:6001。

4

我正在尝试设置Apache 2.4(在Virtualmin上)将wss://sub.domain.com请求转发到ws://localhost:6001,但是我没有成功。我已经遵循了无数的教程,并查看了大量的Stackoverflow问题-但我仍然感到困惑。

我已经安装并启用了proxyproxy_httpproxy_wstunnelrewrite

首先我尝试了:

ServerName sub.domain.com

RewriteEngine on
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*)           ws://127.0.0.1:6001/$1 [P,L]

ProxyPass / http://127.0.0.1:6001/
ProxyPassReverse / http://127.0.0.1:6001/

SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
SSLCertificateChainFile /path/to/chain.pem
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

然后我尝试了:

ServerName sub.domain.com

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*)           ws://localhost:6001/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*)           http://localhost:6001/$1 [P,L]

ProxyPreserveHost on
ProxyPass / ws://localhost:6001/
ProxyPassReverse / ws://localhost:6001/

...ssl directives

几乎涵盖了两者的每种组合。

至于 WebSocket 服务器,我正在使用端口 6001 上的 Laravel-websockets。

我做错了什么?

3个回答

2
  • 从您的配置文件中看来,您的服务器似乎只提供websocket服务。
  • 我仍然建议使用VirtualHost以获得更大的灵活性。
  • 您不需要像现在这样将mod_rewrite与mod_proxy结合使用(除非您想托管其他服务并进行更复杂的路由)。代理部分已足够满足您的需求。

我在云端设置了一个完整的测试来验证这一点。这很简单,但是有效。

<IfModule mod_ssl.c>
<VirtualHost *:443>

    ServerName  sub.domain.com

    ProxyPass "/"  "ws://localhost:6001/"

    #  .... SSL config here, e.g. letsencrypt or else ....
    # I was just running `sudo certbot` to fill this in for me. 

 </VirtualHost>
</IfModule>

我使用一个来自https://github.com/Theldus/wsServer的超级简单的ws服务器进行了测试。

将DNS配置为ws.mydomain.com,然后在wss://ws.mydomain.com上运行https://www.piesocket.com/websocket-tester。成功了。


自从我让这个设置运行起来后,如果你仍然有问题,很可能是由于防火墙或其他配置不正确造成的。请仔细检查您的ws服务器是否正常工作等。 - Ralf Ulrich

0

有人回复了一个答案,但显然已经删除了,在Chrome和Insomnia中下面的代码可以工作(但不适用于Firefox - 但这可能是Laravel-websockets的问题)。重新发布它以帮助其他人:

RewriteCond %{HTTP:Connection} =Upgrade [NC]
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:6001/$1 [P]

您可以将上述内容添加到您的虚拟服务器配置或htaccess文件中。


0
这对我在apache上使用webmin有效,希望能帮到其他人。
<IfModule mod_ssl.c>
<VirtualHost *:443> 
    ProxyPreserveHost On
    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    ProxyRequests Off
    ServerName webmin.[example.com]
    ServerAlias www.webmin.[example.com]
    ProxyPass / https://localhost:10000/
    ProxyPassReverse / https://localhost:10000/
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*)           wss://localhost:10000/$1 [P,L]

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/webmin.[example.com]/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/webmin.[example.com]/privkey.pem
</VirtualHost>
</IfModule>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接