【问题标题】:WebSocket connection to 'wss://test.example.com:8090/' failed: WebSocket opening handshake timed outWebSocket 连接到“wss://test.example.com:8090/”失败:WebSocket 打开握手超时
【发布时间】:2019-06-11 09:30:25
【问题描述】:

我在 Laravel 项目中使用 https 和 websocket 连接。我已经设置了如下虚拟主机:

<VirtualHost *:80>
ServerName test.example.com
ServerAdmin test.example.com
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>

ServerAdmin test.example.com
ServerName test.example.com

SSLEngine on
SSLCertificateFile "cert.pem"
SSLCertificateKeyFile "privkey.pem"
SSLCertificateChainFile "chain.pem"

SSLProxyEngine On
ProxyPreserveHost On
ProxyRequests Off

ProxyPass /wss wss://test.example.com:8090/
ProxyPassReverse /wss wss://test.example.com:8090/

DocumentRoot "/var/www/html/laravel/public"
<Directory "/var/www/html/laravel/public">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride all
    Require all granted
</Directory>
</VirtualHost>

但在浏览器中显示错误:

   WebSocket connection to 'wss://test.example.com:8090/' failed: WebSocket opening handshake timed out

您能帮我们解决什么问题吗?

提前致谢

【问题讨论】:

  • 不清楚您要做什么。看起来您尝试从脚本内部直接访问 wss://test.example.com:8090/,这意味着它不会通过您的 Apache 代理,这使得您提供的 Apache 配置无关紧要。如果您想通过 Apache 代理您的 wss,您应该使用 wss://test.example.com/wss/(不同的端口,不同的路径)。
  • 我正在使用 apache 网络服务器,但我担心的是,即使我没有在我的 vhosts 文件中应用代理传递设置,它也会显示相同的错误,我该如何配置我的服务器以使其正常工作?
  • 同样,不清楚您首先要做什么(直接访问或通过 Apache)。答案取决于您实际想要做什么:通过直接访问,您可能需要打开一些防火墙,并且您还需要确保 websocket 服务器首先接受来自外部的请求(听 0.0.0.0 vs . 仅在 127.0.0.1 上收听)。当与 Apache 一起使用时,您需要确保使用正确的 URL,正如我在上一条评论中已经解释的那样。并且,如果您指定了 wss://,请确保您的 WS 服务器实际上正在执行 wss:// 而不仅仅是 ws://
  • 好的,websokcet 服务器在 ws 上运行良好,因为如果删除 ssl(443) 的配置,那么它在 http 上运行良好。 Websocket 服务器也接受来自外部的请求。我可以知道如何检查 WS 服务器是否在运行 wss:// 而不仅仅是 ws://
  • 我认为您正在将您的 websocket 服务器(端口 8090)与您的 Apache 作为反向代理(端口 443 和 80)混合。再说一次 - 你实际上想要做什么仍然未知 - 直接访问或使用 Apache。

标签: ssl websocket apache2 vhosts ratchet


【解决方案1】:

如果你使用 laravel 库 cboden/ratchet 作为套接字 然后转到 app/Console/Commands/WebSocketServer.php 用这些代码行替换句柄函数并使用您的认证文件路径'local_cert'和'local_pk'

public function handle()
{

    $loop   = Factory::create();
    $webSock = new SecureServer(
        new Server('0.0.0.0:8090', $loop),
        $loop,
        array(
            'local_cert'        => '/opt/ssl/chamberstock_com.crt', // path to your cert
            'local_pk'          => '/opt/ssl/server.key', // path to your server private key
            'allow_self_signed' => TRUE, // Allow self signed certs (should be false in production)
            'verify_peer' => FALSE
        )
    );
    // Ratchet magic
    $webServer = new IoServer(
        new HttpServer(
            new WsServer(
                new WebSocketController()
            )
        ),
        $webSock
    );
    $loop->run();

}

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2023-03-08
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2017-05-09
    相关资源
    最近更新 更多