node.js - websocket 服务器的 Apache 代理不工作

标签 node.js apache ssl websocket

所以我正在尝试将我的 websocket 服务器升级到 HTTPS,但我遇到了一些麻烦。在我开始使用 SSL&Co 之前,服务器本身就可以正常工作。 websocket 服务器是用 node 编写的,仅服务于 websocket,前端服务器是 Apache 并服务于客户端应用程序。

WebSocket 端(基本上)是:

const websock = require('./node_modules/ws');
const fs = require('fs');

console.log("Starting VoxelatedAvacado server.");
const server = require('https').createServer({
    cert: fs.readFileSync('/etc/letsencrypt/live/domain/cert.pem'),
    key: fs.readFileSync('/etc/letsencrypt/live/domain/privkey.pem'),
    port: 36245
});
const wss = new websock.Server({ server: server, path: "/ws"});

Apache 虚拟主机是

<VirtualHost *:443>
ServerName dev.domain
DocumentRoot /var/www/domain/client_dev

    <Directory /var/www/domain/client_dev/>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    CustomLog ${APACHE_LOG_DIR}/access.log common_complete

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml $
    </IfModule>

SSLEngine on
SSLProxyEngine on
SSLCertificateFile /etc/letsencrypt/live/domain/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/domain/fullchain.pem

<Location "/ws/">
    ProxyPass "wss://localhost:36245/ws"
</Location>

<FilesMatch "\.(cgi|shtml|phtml_php)$">
    SSLOptions +StdEnvVars
</FilesMatch>
</VirtualHost>

测试客户端有以下行:ws = new WebSocket("wss://dev.domain/ws"); 当该行被触发时,Chrome 会说一个威胁性的 WebSocket与“wss://dev.domain/ws”的连接失败:WebSocket 握手期间出错:意外的响应代码:404。没有行打印到 Apache 错误日志。

感谢您的帮助!

最佳答案

如果你使用会发生什么:

ProxyPass "wss://localhost:36245/ws/

也就是匹配尾随的'/'

关于node.js - websocket 服务器的 Apache 代理不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531528/

相关文章:

apache - 对某些页面强制使用 https,对所有其他页面强制使用 http

javascript - 找不到 babel 运行时库

node.js - 如何导入同名的 Node 模块?

node.js - 如何更改 Jenkins 上的 Node.js 版本?

node.js - Web 服务器和 Node.js?

c# - 在 C# 中允许 CX509PrivateKeyClass 的 KeyProtection

javascript - 如何检查 DiscordJS V13 中是否有人在语音 channel 中?

php - REMOTE_ADDR 为空,不包含在 SERVER 数组中

java - 消化器 : Extracting node name

PHP session 丢失,IIS 中的多个站点