Laravel Reverb在Apache生产环境中无法运行的问题及解决方法
问题描述
我在生产环境中使用Apache + AWS Lightsail + LAMP 8.2部署Laravel Reverb时遇到问题(本地环境一切正常)。
服务版本信息
- Reverb版本:@beta
- Laravel版本:^11.0
- PHP版本:8.2
我的.env配置
BROADCAST_CONNECTION=reverb REVERB_SERVER_HOST=0.0.0.0 REVERB_SERVER_PORT=8080 REVERB_APP_ID=xxx REVERB_APP_KEY=xxx REVERB_APP_SECRET=xxx REVERB_HOST="mydomain.com" 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}"
routes/channel.php代码
Broadcast::channel('update_coins.{id}', function ($user, $id) { return (int) $user->id === (int) $id; });
resources/js/echo.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'], });
虚拟主机配置
boost-https-vhost.conf
<VirtualHost *:443> SSLEngine on SSLCertificateFile "/opt/bitnami/apache/conf/server.crt" SSLCertificateKeyFile "/opt/bitnami/apache/conf/server.key" DocumentRoot "/opt/bitnami/apache/htdocs/boost/public" <Directory "/opt/bitnami/apache/htdocs/boost/public"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ProxyPass "/app" "ws://0.0.0.0:8080/app" ProxyPassReverse "/app" "ws://0.0.0.0:8080/app" </VirtualHost>
boost-vhost.conf
<VirtualHost *:80> DocumentRoot "/opt/bitnami/apache/htdocs/boost/public" <Directory "/opt/bitnami/apache/htdocs/boost/public"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
.htaccess配置
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # ROUTER WWW Redirect. RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # ROUTER HTTPS Redirect RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Send Requests To Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
测试与错误
执行以下测试代码时:
$user_id = 1; $user = User::find($user_id); $user->update(['coins' => $user->coins + 100]); UpdateCoinsEvent::dispatch($user);
抛出错误:
local.ERROR: Pusher error: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Not Found</title> {"exception":"[object] (Illuminate\Broadcasting\BroadcastException(code: 0): Pusher error: <!DOCTYPE html> <html lang=\"en\"> <head> <meta charset=\"utf-8\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <title>Not Found</title>
复现步骤
通过Artisan命令复现:
protected $signature = 'coins'; protected $description = 'Command description'; public function handle() { $user_id = 1; $user = User::find($user_id); $user->update(['coins' => $user->coins + 100]); UpdateCoinsEvent::dispatch($user); }
执行命令后出现相同的Not Found错误信息。
解决方案
按以下步骤操作即可解决问题:
创建子域名:在域名服务商(如Cloudflare)配置子域名(开启或关闭代理均可),例如
ws.example.com。修改
.env配置:
BROADCAST_CONNECTION=reverb REVERB_SERVER_HOST=0.0.0.0 REVERB_SERVER_PORT=8080 REVERB_APP_ID=your id REVERB_APP_KEY=your key REVERB_APP_SECRET=your secret REVERB_HOST=0.0.0.0 REVERB_PORT=8080 REVERB_SCHEME=http VITE_REVERB_APP_KEY="${REVERB_APP_KEY}" VITE_REVERB_HOST="ws.example.com" VITE_REVERB_PORT=443 VITE_REVERB_SCHEME=https
- 配置子域名虚拟主机
创建ws-vhost.conf:
<VirtualHost *:80> ServerName ws.example.com ProxyPreserveHost on ProxyPass "/app" "ws://0.0.0.0:8080/app" ProxyPassReverse "/app" "ws://0.0.0.0:8080/app" </VirtualHost>
创建ws-https-vhost.conf:
<VirtualHost *:443> ServerName ws.example.com SSLProxyEngine on SSLEngine on SSLCertificateFile "/opt/bitnami/apache/conf/server.crt" SSLCertificateKeyFile "/opt/bitnami/apache/conf/server.key" ProxyPreserveHost on ProxyPass "/app" "ws://0.0.0.0:8080/app" ProxyPassReverse "/app" "ws://0.0.0.0:8080/app" </VirtualHost>
- 编译前端与清理缓存
执行以下命令:
npm run build php artisan cache:clear php artisan route:clear
注意:reverb.php和broadcasting.php配置文件无需修改。
- 启用必要Apache模块
确保启用以下模块:
mod_proxymod_proxy_wstunnelmod_ssl
内容的提问来源于stack exchange,提问作者Wilian Maique
相关产品推荐
相关产品推荐

