laravel websocket 无法在生产环境中连接
laravel websocket won't connect on production
我使用 Beyondcode websockets 在我的本地主机上创建了一个 laravel 网站。一切正常,直到我尝试将我的网站上传到实时服务器。我一直在互联网上搜索解决方案,但找不到任何适合我的方法。问题是当 websocket 尝试连接时,它 returns 什么也没有。我认为这与我的 NGINX 配置有关。
在我的本地主机上,websocket 可以通过 ws://127.0.0.1/... 正常连接,但在生产环境中 ws:// 和 wss:// 都不起作用。
我在子域上得到了主管 运行 命令 php artisan websockets:serve
。它只是不连接。
我的代码:
config/broadcasting.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'),
'host' => 'socket.website.com',
'port' => "",
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
recources/js/bootstrap.js
broadcaster: 'pusher',
key: 'key12',
wsHost: 'socket.website.com',
wssHost: 'socket.website.com',
wsPort: "",
wssPort:"",
forceTLS: true,
disableStats: true,
forceTLS: true,
enabledTransports: ['ws', 'wss']
vHost 会议
server {
listen 80;
server_name socket.website.com;
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;
}
}
docRoot $VH_ROOT
vhDomain $VH_NAME
vhAliases www.$VH_NAME
adminEmails info@proxeuse.com
enableGzip 1
enableIpGeo 1
errorlog $VH_ROOT/logs/$VH_NAME.error_log {
useServer 0
logLevel ERROR
rollingSize 10M
}
accesslog $VH_ROOT/logs/$VH_NAME.access_log {
useServer 0
logFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""
logHeaders 5
rollingSize 10M
keepDays 10
compressArchive 1
}
scripthandler {
add lsapi:socke9530 php
}
phpIniOverride {
}
rewrite {
enable 1
autoLoadHtaccess 1
}
vhssl {
keyFile /etc/letsencrypt/live/socket.website.com/privkey.pem
certFile /etc/letsencrypt/live/socket.website.com/fullchain.pem
certChain 1
sslProtocol 24
ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES128-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA128:DHE-RSA-AES128-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA128:ECDHE-RSA-AES128-SHA384:ECDHE-RSA-AES128-SHA128:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA384:AES128-GCM-SHA128:AES128-SHA128:AES128-SHA128:AES128-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4
enableECDHE 1
renegProtection 1
sslSessionCache 1
enableSpdy 15
enableStapling 1
ocspRespMaxAge 86400
}
我不知道我做错了什么。谁能帮帮我?
Laravel 生产中的 websocket 通过 subdomain
和 nginx
步骤 1.
在后台代码中创建 websocket conf 文件到 运行 就像
在/etc/supervisor/conf.d
(Debian/Ubuntu)或/etc/supervisord.d
(红色Hat/CentOS)
创建文件 websockets.conf
然后把这个代码
[program:websockets]
command=/usr/bin/php <laravel-path>/artisan websockets:serve
numprocs=1
autostart=true
autorestart=true
user=<username>
- 然后 运行
supervisorctl update
和 supervisorctl start websockets
In the step your laravel websocket will start in backgroud at port 6001
第 2 步
- 创建一个类似
socket.example.com
的子域
- 现在通过
nginx
代码 nginx
配置连接到此域到端口 6001
server {
listen 80;
server_name socket.example.com;
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;
}
}
In this setup your subdomain is ready to accept websocket request but without ssl
步骤 3.
- 安装 certbot 以获得此域的免费 ssl
- 一旦安装
certbot
只需 运行 sudo certbot
然后你会提示 select 一个域来获取 ssl
In this step your geting ssl for you subdomain now you are ready to accept wss
connection on this port
第 4 步
因为您的 wss
已在您的子域 wss://socket.exmaple.com
上准备就绪,您需要更改 laravel-echo
(客户端) 和 laravel-websocket
(服务器)
在bootstrap.js
wsHost: <subdomainname>,
wssHost: <subdomainname>,
wsPort: "",
wssPort:"",
forceTLS: true,
- 在
config/broadcasting.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'),
'host' => <subdomain>,// replace this with your subdomain
'port' => "",
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
In this setup we are connection websocket to subdomain and here port is empty because, subdomain is proxy of port 6001
You can verify your wss
is working in https://www.websocket.org/echo.html in here just copy <subdomain>/app/broadcasting?protocol=7&client=js&version=7.0.3&flash=false
引用链接
https://beyondco.de/docs/laravel-websockets/basic-usage/starting
https://beyondco.de/docs/laravel-websockets/basic-usage/ssl#usage-with-a-reverse-proxy-like-nginx
我使用 Beyondcode websockets 在我的本地主机上创建了一个 laravel 网站。一切正常,直到我尝试将我的网站上传到实时服务器。我一直在互联网上搜索解决方案,但找不到任何适合我的方法。问题是当 websocket 尝试连接时,它 returns 什么也没有。我认为这与我的 NGINX 配置有关。
在我的本地主机上,websocket 可以通过 ws://127.0.0.1/... 正常连接,但在生产环境中 ws:// 和 wss:// 都不起作用。
我在子域上得到了主管 运行 命令 php artisan websockets:serve
。它只是不连接。
我的代码:
config/broadcasting.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'),
'host' => 'socket.website.com',
'port' => "",
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
recources/js/bootstrap.js
broadcaster: 'pusher',
key: 'key12',
wsHost: 'socket.website.com',
wssHost: 'socket.website.com',
wsPort: "",
wssPort:"",
forceTLS: true,
disableStats: true,
forceTLS: true,
enabledTransports: ['ws', 'wss']
vHost 会议
server {
listen 80;
server_name socket.website.com;
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;
}
}
docRoot $VH_ROOT
vhDomain $VH_NAME
vhAliases www.$VH_NAME
adminEmails info@proxeuse.com
enableGzip 1
enableIpGeo 1
errorlog $VH_ROOT/logs/$VH_NAME.error_log {
useServer 0
logLevel ERROR
rollingSize 10M
}
accesslog $VH_ROOT/logs/$VH_NAME.access_log {
useServer 0
logFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""
logHeaders 5
rollingSize 10M
keepDays 10
compressArchive 1
}
scripthandler {
add lsapi:socke9530 php
}
phpIniOverride {
}
rewrite {
enable 1
autoLoadHtaccess 1
}
vhssl {
keyFile /etc/letsencrypt/live/socket.website.com/privkey.pem
certFile /etc/letsencrypt/live/socket.website.com/fullchain.pem
certChain 1
sslProtocol 24
ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES128-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA128:DHE-RSA-AES128-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA128:ECDHE-RSA-AES128-SHA384:ECDHE-RSA-AES128-SHA128:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA384:AES128-GCM-SHA128:AES128-SHA128:AES128-SHA128:AES128-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4
enableECDHE 1
renegProtection 1
sslSessionCache 1
enableSpdy 15
enableStapling 1
ocspRespMaxAge 86400
}
我不知道我做错了什么。谁能帮帮我?
Laravel 生产中的 websocket 通过 subdomain
和 nginx
步骤 1.
在后台代码中创建 websocket conf 文件到 运行 就像
在
/etc/supervisor/conf.d
(Debian/Ubuntu)或/etc/supervisord.d
(红色Hat/CentOS) 创建文件websockets.conf
然后把这个代码
[program:websockets]
command=/usr/bin/php <laravel-path>/artisan websockets:serve
numprocs=1
autostart=true
autorestart=true
user=<username>
- 然后 运行
supervisorctl update
和supervisorctl start websockets
In the step your laravel websocket will start in backgroud at port
6001
第 2 步
- 创建一个类似
socket.example.com
的子域
- 现在通过
nginx
代码nginx
配置连接到此域到端口6001
server {
listen 80;
server_name socket.example.com;
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;
}
}
In this setup your subdomain is ready to accept websocket request but without ssl
步骤 3.
- 安装 certbot 以获得此域的免费 ssl
- 一旦安装
certbot
只需 运行sudo certbot
然后你会提示 select 一个域来获取 ssl
In this step your geting ssl for you subdomain now you are ready to accept
wss
connection on this port
第 4 步
因为您的
wss
已在您的子域wss://socket.exmaple.com
上准备就绪,您需要更改laravel-echo
(客户端) 和laravel-websocket
(服务器)在
bootstrap.js
wsHost: <subdomainname>,
wssHost: <subdomainname>,
wsPort: "",
wssPort:"",
forceTLS: true,
- 在
config/broadcasting.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'),
'host' => <subdomain>,// replace this with your subdomain
'port' => "",
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
In this setup we are connection websocket to subdomain and here port is empty because, subdomain is proxy of port
6001
You can verify your
wss
is working in https://www.websocket.org/echo.html in here just copy<subdomain>/app/broadcasting?protocol=7&client=js&version=7.0.3&flash=false
引用链接
https://beyondco.de/docs/laravel-websockets/basic-usage/starting https://beyondco.de/docs/laravel-websockets/basic-usage/ssl#usage-with-a-reverse-proxy-like-nginx