NodeJS + Apache 代理,在通过路由安全的 websocket 时遇到问题(请提供 apache 配置帮助)

NodeJS + Apache Proxy, having trouble with routing a secured websocket through (apache config help please)

我在这里浏览了无数关于如何执行此操作的帖子,但我无法完成这个简单的任务。我使用 Apache 作为代理,假设 encrypt/decrypt TSL 数据包,然后与 NodeJS 服务器进行不安全的通信。问题是 web 应用程序加载正常,但是 websocket 失败并出现错误 404(未找到)并且我之前遇到过错误 200(握手问题)。我尝试了很多不同的配置,但都没有用。

这是我在客户端的。

const connection = new WebSocket('wss://example.com/wss'); 

在这里使用 wss:// 而不是 ws:// 因为我想要一个安全的连接。 在服务器端,我只有标准

ws.on('connection', w => {
    w.on('message', msg => {
        var data = JSON.parse(msg);
        // do something with the message ... 
    });
});

这是我的 Apache 配置文件

ServerName www.example.com                                                                                                                                                                                                                                                                                                                                                                                                                                                     RewriteEngine on
RewriteCond %{HTTP:Upgrade}=websocket [NC]
RewriteRule /(.*) wss://127.0.0.1:3000/ [P,L]

SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLProxyEngine on

ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
ProxyPass /wss ws://127.0.0.1:3000/
ProxyPassReverse /wss ws://127.0.0.1:3000/

在 Apache 方面,我不确定我是否应该为 ProxyPass 使用 wss://ws://,因为它在 Apache 和 NodeJS 之间应该是不安全的。我在这里把它写成 ws:// 但两种方式都不起作用。对于此配置,我收到 404 错误。

如有任何帮助,我们将不胜感激!谢谢

我用Nginx反向代理解决了这个问题,

问题是我将套接字连接设置在端口 8080 上,但代理设置为端口 3000。如果将它们放在同一端口上,问题应该会解决。

即 应用程序服务器,

const ws = new WebSocket.Server({port: 3001});

阿帕奇

ProxyPass /wss ws://127.0.0.1:3001/
ProxyPassReverse /wss ws://127.0.0.1:3001/