【发布时间】:2022-01-08 15:25:42
【问题描述】:
我正在尝试用多个服务器实现 Laravel Websockets。
我有一个应用服务器和一个队列工作服务器正在运行。我尝试从 Queue Worker 服务器广播我的通知,但我收到了
lluminate\Broadcasting\BroadcastException: Pusher 错误: 。在/home/forge/my-app/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:128
在 App Server 的 Network 选项卡中,我可以确认它已连接到 websocket。我使用了alex bouma's post 并设置了反向代理。 如果我在 App Server 中广播,它可以工作:
server {
listen 6002 ssl http2;
listen [::]:6002 ssl http2;
server_name example.com;
index.php
location / {
proxy_pass http://127.0.0.1:6001;
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;
}
}
App Server 和 Queue Worker 位于专用网络中,可以相互连接。但是,当我尝试从 Queue Worker 服务器进行广播时,它不起作用。
我很难理解如何让它发挥作用。
-
我是否还必须在队列工作程序上运行
php artisan websockets:serve?如果是这样,我需要给--host={private-ip}标志吗? -
在我的
broadcasting.php中,我已将应用服务器的私有 ip 添加为PUSHER_ENDPOINT_HOST用于 Queue Worker 的环境。这是正确的吗?
'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'),
'encrypted' => in_array(config('app.env'), ['production', 'staging']),
'host' => env('PUSHER_ENDPOINT_HOST', '127.0.0.1'),
'port' => 6001,
'scheme' => 'http'
],
],
更新:我按照@KamleshPaul 的建议进行了更改。下面是我所有代码的样子。
我在应用服务器和工作服务器中都有这些配置。我在两者上都运行php artisan websockets:serve,并且App Server 成功连接到套接字。但是,队列工作人员不会发送通知。
php artisan websockets:serve 均显示:“正在端口 6001 上启动 WebSocket 服务器...”
但它似乎仍然不起作用。 (顺便说一句,我使用的是 Laravel Forge,允许的端口是:
-
应用:6001、6002、433、22
-
工人:6001、6002、22
.env
PUSHER_APP_ID=aaa
PUSHER_APP_KEY=bbb
PUSHER_APP_SECRET=ccc
PUSHER_APP_CLUSTER=mt1
PUSHER_HOST=socket.my_domain.com
PUSHER_PORT=433
PUSHER_SCHEME=https
VITE_PUSHER_APP_KEY=${PUSHER_APP_KEY}
VITE_PUSHER_APP_CLUSTER=${PUSHER_APP_CLUSTER}
VITE_PUSHER_HOST=${PUSHER_HOST}
VITE_PUSHER_SCHEME={PUSHER_SCHEME}
VITE_PUSHER_PORT={PUSHER_PORT}
echo.js
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
wsHost: import.meta.env.VITE_PUSHER_HOST,
wsPort: import.meta.env.VITE_PUSHER_PORT || 443,
forceTLS: true,
disableStats: true,
scheme: import.meta.env.VITE_PUSHER_SCHEME,
enabledTransports: ["ws", "wss"],
}
广播.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'),
'encrypted' => true,
'host' => env('PUSHER_HOST'),
'port' => env('PUSHER_PORT'),
'scheme' => env('PUSHER_SCHEME')
],
],
socket.my_domain.com的nginx
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/socket.my_domain.com/before/*;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name socket.my_domain.com;
server_tokens off;
root /home/forge/socket.my_domain.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/socket.my_domain.com/1262458/server.crt;
ssl_certificate_key /etc/nginx/ssl/socket.my_domain.com/1262458/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/socket.my_domain.com/server/*;
location / {
proxy_pass http://127.0.0.1:6001;
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;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/socket.my_domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/socket.my_domain.com/after/*;
【问题讨论】:
-
你需要在
broadcasting.php中设置正确的host,port和scheme -
假设我有独立运行的 AppServer 10.0.0.1:6001 (https)。还有 QueueServer 10.0.03。正确的
host、port和scheme是什么? -
我不认为在端口
6001中可以启用ssl?,通常我在子域中使用端口433,例如socket.appname.com -
我的应用服务器块有端口 443。 6001 是 6002 端口的反向代理。你之前在多个实例中设置了 laravel websockets 吗?
-
是的,我的
websockets是独立的 nginx 配置
标签: laravel websocket laravel-8 laravel-websockets laravel-horizon