所以我正在尝试将我的 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/