LARAVEL:WebSocket 在连接建立之前关闭

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

我正在使用 Composer v 2.7.6 并创建了一个 Laravel 项目。之后,我在 Laravel 项目中使用 Pusher 配置了 Websocket。 Websocket 在我的本地设备(localhost)中工作正常

尝试访问

localhost:8000/laravel-websockets
并且可以成功连接。

现在我尝试使用 Nginx 在我的临时环境中托管它。 Nginx 配置:

location /app/ {
                proxy_pass http://localhost:6001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

当我尝试连接 websocket 时,它给了我问题

pusher.min.js:8 WebSocket connection to 'ws://xxx.xxx.xxx.xxx:6001/app/7fd7c6de51d232280348?protocol=X&client=js&version=x.x.x&flash=false' failed: WebSocket is closed before the connection is established.

请帮忙。如果您有任何文档,也会有很大帮助。谢谢你

laravel websocket
1个回答
0
投票

您可以尝试以下几个步骤来解决该问题:

  • 验证 Nginx 的设置:验证 Nginx 的配置是否允许 WebSocket 连接。可以将以下行添加到您的 Nginx 配置文件中:
    http {
    ...
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server localhost:6001;
    }

    server {
        listen 80;
        server_name example.com;

        location /app/ {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

WebSocket 连接已启用,并使用此配置设置上游服务器。

  • 验证 Laravel WebSocket 配置:确定其设置正确。为了确保推送器设置配置正确,请检查 config/websockets.php 文件。

  • 验证 Pusher 配置:验证 API 密钥和凭据是否正确,并仔细检查您的 Pusher 配置。 验证防火墙规则:确保登台服务器的防火墙规则允许端口 6001 上的入站 WebSocket 连接。

  • 检查 WebSocket 日志:要查看是否存在任何问题或故障,请查看 WebSocket 日志。在您的 Laravel 项目中,运行命令 php artisan websocket:logs 来完成此操作。

以下有用的资源可以帮助您解决问题: Laravel WebSocket 文档

© www.soinside.com 2019 - 2024. All rights reserved.