Laravel SSL 配置的 Websockets 不工作

Laravel Websockets with SSL Configuration not Working

Laravel 不使用 Https 的 Websockets 在没有 ssl 配置的情况下工作正常尝试了文档中找到的所有内容 link https://beyondco.de/docs/laravel-websockets/basic-usage/ssl 但没有任何效果

这是我的 websockets 配置

brodcasting.php

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

bootstrap.js

window.Echo = new Echo({
        broadcaster: 'pusher',
        key: process.env.MIX_PUSHER_APP_KEY,
        wsHost: window.location.hostname,
        wsPort: 6001,
        forceTLS: false,
        disableStats: true,
        enabledTransports: ['ws','wss'],
        authorizer: (channel, options) => {
            return {
                authorize: (socketId, callback) => {
                    axios.post('/api/broadcasting/auth', {
                        socket_id: socketId,
                        channel_name: channel.name
                    }).then(({data}) => {
                        callback(false, data);

                    }).catch(error => {
                        callback(true, error);
                    });
                }
            };
        }
    }); 

.env

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT=/etc/letsencrypt/live/xxxxxxxx/fullchain.pem
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK=/etc/letsencrypt/live/xxxxxxxxxx/privkey.pem
 

websockets.php

'ssl' => [

        'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),

        'capath' => env('LARAVEL_WEBSOCKETS_SSL_CA', null),

        'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),

        'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', ''),

        'verify_peer' => env('APP_ENV') === 'production',

        'allow_self_signed' => env('APP_ENV') !== 'production',

    ],

我已经为我找到了解决方案,问题是 LetsEncrypt ssl 在

中做了一些更改

在default.conf中添加在sites-available

# Added this for Web Sockets Proxy 
    location /ws/ {
     proxy_pass             http://127.0.0.1:6001;
     proxy_set_header Host  $host;
     proxy_read_timeout     60;
     proxy_connect_timeout  60;
     proxy_redirect         off;

    # Allow the use of websockets
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection 'upgrade';
     proxy_set_header Host $host;
     proxy_cache_bypass $http_upgrade;
    }

在主管中改变了

user=root
commad=sudo /usr/bin/php /var/www/html/TestFolder/artisan websockets:serve
[program:websockets]
command=sudo /usr/bin/php /var/www/html/TestFolder/artisan websockets:serve
numprocs=1
autostart=true
autorestart=true
user=root
redirect_stderr=true

之后

    sudo supervisorctl update websockets
    sudo supervisorctl restart all

效果神奇,希望对您有所帮助!