如何在 Laravel-websockets 中使用 CloudFlare SSL



我的项目托管在DigitalOcean中,我已经使用Cloudflare将域连接到服务器。 我使用SSL。但是 WSS 连接不起作用。我收到此错误消息WebSocket connection to 'wss://mywebsite.com/app/ABCDEFG?protocol=7&client=js&version=6.0.3&flash=false' failed: Error during WebSocket handshake: Unexpected response code: 404

我有 apache2 服务器

我的配置/广播.php看起来像

'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => false,
'encrypted'=>true,
'host'=>'127.0.0.1',
'port'=>2053,
'scheme'=>'https',
'curl_options'=> [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],

和客户端 引导.js看起来像

window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: false,
encrypted:true,
wsHost: window.location.hostname,
wsPort: 2053,
wssPort: 2053,
disableStats: true,
enabledTransports:['ws','wss'],
});

我运行命令php artisan websockets:serve --port=2053

如果您需要任何其他信息,请写评论。 有人有解决方案吗? 谢谢

新错误failed: WebSocket is closed before the connection is established.

您需要配置 Apache Proxy Web Socket Tunnel 模块或 Proxy Http 模块: https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html https://httpd.apache.org/docs/2.4/mod/mod_proxy_http.html

当检查连接和代理将其传递给 WS 后端时:

RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://127.0.0.1:2053/$1" [P,L]

最新更新