尝试通过 SSL apache 上的代理连接到 websocket 时获得响应 301

问题描述 投票:0回答:0

我正在尝试在现有的 apache 服务器上使用 node.js 设置一个 websocket 服务器。由于服务器是 SSL,我还需要一个 websocket 来保护。为此,我正在尝试实施代理解决方案。我以这种方式通过 vhost 重定向任何 websocket 请求:

<VirtualHost 127.0.0.1:80>
  ServerName status.localhost
  <Location /server-status>
    Require local
    SetHandler server-status
  </Location>
  
    RewriteEngine On
  
    # When Upgrade:websocket header is present, redirect to ws
    # Using NC flag (case-insensitive) as some browsers will pass Websocket
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*)    ws://localhost:8888/$1 [P,L]
    
    # All other requests go to http
    ProxyPass "/" "http://localhost:443/"
    
</VirtualHost>

检查 mod_proxy_wstunnel.so 和 mod_proxy.so 是否加载到 httpd.conf.

然后我拿了一个简单的nodejs websocket实现示例并运行它

// Importing the required modules
const WebSocketServer = require('ws');
 
// Creating a new websocket server
const wss = new WebSocketServer.Server({ port: 8888 })
 
// Creating connection using websocket
wss.on("connection", ws => {
    console.log("new client connected");
 
    // sending message to client
    ws.send('Welcome, you are connected!');
 
    //on message from client
    ws.on("message", data => {
        console.log(`Client has sent us: ${data}`)
    });
 
    // handling what to do when clients disconnects from server
    ws.on("close", () => {
        console.log("the client has connected");
    });
    // handling client connection error
    ws.onerror = function () {
        console.log("Some Error occurred")
    }
});
console.log("The WebSocket server is running on port 8888");

并尝试使用此客户端连接:

const WebSocket = require('ws');
s = new WebSocket("wss://hw-assist.org:443");
s.onopen = function() {console.log("OPENED")};

然而,当我尝试连接时,我得到以下信息:

Error: Unexpected server response: 301
    at ClientRequest.<anonymous> (/home/bitnami/jstest/node_modules/ws/lib/websocket.js:888:7)
    at ClientRequest.emit (events.js:314:20)
    at HTTPParser.parserOnIncomingClient (_http_client.js:601:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:122:17)
    at TLSSocket.socketOnData (_http_client.js:474:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (/home/bitnami/jstest/node_modules/ws/lib/websocket.js:1004:13)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

我错过了什么?谢谢!

node.js apache websocket mod-proxy mod-proxy-wstunnel
© www.soinside.com 2019 - 2024. All rights reserved.