You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Reverb在Apache生产环境SSL配置后无法正常工作求助

Laravel Reverb 连接错误排查(HTTPS环境)

环境信息

  • PHP 8.3.12
  • Laravel 11.29.0
  • AA面板
  • Let's Encrypt SSL证书
  • 执行php artisan reverb:start --debug显示服务器启动在0.0.0.0:8080 (example.com),但前端receive-notification视图控制台出现连接错误

相关配置代码

事件类代码(SendNotification.php)

class SendNotification implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

     public $msg;

    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    public function broadcastOn()
    {
        return new Channel('noti');
    }

    public function broadcastAs()
    {
        return "notify";
    }

    public function broadcastWith()
    {
        return [
            "message" => $this->msg
        ];
    }
}

.env 配置

REVERB_APP_ID=APP_ID
REVERB_APP_KEY=APP_key
REVERB_APP_SECRET=APP_SECRET
REVERB_HOST="example.com"
REVERB_SERVER_PORT=8080
REVERB_PORT=443
REVERB_SCHEME=https

VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"

web.php 路由配置

Route::get('receive-notification', function(){
    return view('receive-notification');
});    
Route::get('/send-notification/{msg}', function($msg){
    echo "Notification Message=>".$msg;
    event(new SendNotification($msg));
});

echo.js 配置(resources/js/app.js)

import Echo from 'laravel-echo';

import Pusher from 'pusher-js';
window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
    wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
    forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
    enabledTransports: ['ws', 'wss'],
});

receive-notification.blade.php 前端代码

@vite('resources/js/app.js')
<script type="module">
   window.Echo.channel("noti").
    listen(".notify", (e) => {
      console.log(e);
    } )
</script>

Apache 虚拟主机配置

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot "/www/wwwroot/example.com/public"
    ServerName ea68ca5f.example.com
    ServerAlias example.com www.example.com
    #errorDocument 404 /404.html
    ErrorLog "/www/wwwlogs/example.com-error_log"
    CustomLog "/www/wwwlogs/example.com-access_log" combined
    #HTTP_TO_HTTPS_START
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule (.*) https://%{SERVER_NAME}$1 [L,R=301]
    </IfModule>
    #HTTP_TO_HTTPS_END

    #DENY FILES
     <Files ~ (\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)$>
       Order allow,deny
       Deny from all
    </Files>
    
    #PHP
    <FilesMatch \.php$>
            SetHandler "proxy:unix:/tmp/php-cgi-83.sock|fcgi://localhost"
    </FilesMatch>
    
    #PATH
    <Directory "/www/wwwroot/example.com/public">
        SetOutputFilter DEFLATE
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.php index.html index.htm default.php default.html default.htm
    </Directory>

    
</VirtualHost>
<VirtualHost *:443>
    ServerAdmin webmaster@example.com
    DocumentRoot "/www/wwwroot/example.com/public"
    ServerName SSL.example.com
    ServerAlias www.example.com example.com 
    #errorDocument 404 /404.html
    ErrorLog "/www/wwwlogs/example.com-error_log"
    CustomLog "/www/wwwlogs/example.com-access_log" combined
    
    #SSL
    SSLEngine On
    SSLCertificateFile /www/server/panel/vhost/cert/example.com/fullchain.pem
    SSLCertificateKeyFile /www/server/panel/vhost/cert/example.com/privkey.pem
    SSLCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
    SSLProtocol All -SSLv2 -SSLv3 -TLSv1
    SSLHonorCipherOrder On
    
    
    #PHP
    <FilesMatch \.php$>
            SetHandler "proxy:unix:/tmp/php-cgi-83.sock|fcgi://localhost"
    </FilesMatch>
    

    #DENY FILES
     <Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$>
       Order allow,deny
       Deny from all
    </Files>

    #PATH
    <Directory "/www/wwwroot/example.com/public">
        SetOutputFilter DEFLATE
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.php index.html index.htm default.php default.html default.htm
    </Directory>
</VirtualHost>
ProxyPreserveHost On
<Location /app>
        ProxyPass ws://0.0.0.0:8080/app
        ProxyPassReverse ws://0.0.0.0:8080/app
</Location>
<Location /apps>
        ProxyPass http://0.0.0.0:8080/apps
        ProxyPassReverse http://0.0.0.0:8080/apps
</Location>

问题修复步骤

1. 修正Apache SSL虚拟主机的ServerName

当前SSL虚拟主机ServerName设为SSL.example.com,会导致证书匹配失败,改为与域名一致:

<VirtualHost *:443>
    ServerName example.com
    # 其余配置保持不变
</VirtualHost>

2. 调整Proxy配置适配HTTPS

前端使用HTTPS,ProxyPass需要改用wss://协议,并将配置移入SSL虚拟主机内部:

<VirtualHost *:443>
    # 原有SSL配置...

    ProxyPreserveHost On
    <Location /app>
            ProxyPass wss://127.0.0.1:8080/app
            ProxyPassReverse wss://127.0.0.1:8080/app
    </Location>
    <Location /apps>
            ProxyPass https://127.0.0.1:8080/apps
            ProxyPassReverse https://127.0.0.1:8080/apps
    </Location>
</VirtualHost>

3. 优化Echo前端配置

区分ws/wss端口,强制使用wss协议,并指定Reverb代理路径:

window.Echo = new Echo({
    broadcaster: 'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    wsPort: 80,
    wssPort: 443,
    forceTLS: true,
    enabledTransports: ['wss'],
    path: '/app',
});

4. 重启服务

  • 重启Apache服务器
  • 重新启动Reverb服务:php artisan reverb:start

5. 验证功能

访问https://example.com/send-notification/test,查看receive-notification页面控制台是否能收到消息。


内容的提问来源于stack exchange,提问作者Pranav Mankar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:15:54