【问题标题】:laravel websocket won't connect on productionlaravel websocket 不会在生产中连接
【发布时间】:2021-03-12 09:09:25
【问题描述】:

我使用 Beyondcode websockets 在我的本地主机上创建了一个 laravel 网站。一切正常,直到我尝试将我的网站上传到实时服务器。我一直在互联网上寻找解决方案,但找不到任何适合我的东西。问题是当 websocket 尝试连接时,它什么也没有返回。我认为这与我的 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']

虚拟主机会议
    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
}

我不知道我做错了什么。谁能帮帮我?

【问题讨论】:

  • 对于生产最佳解决方案是创建新的子域,如 socket.example.com 并使用端口 433 而不是 6001 这很容易
  • socket.elitewagers.proxweb.eu 它有 ssl 吗?与否
  • Supervisor 将运行php artisan websocket:serve,因此它将在端口6001 上运行websocket,然后通过nginx 反向代理到该子域,然后在websocket 中你不需要放置wsPort: 6001,
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: laravel websocket laravel-echo


【解决方案1】:

通过subdomainnginx 在生产中使用Laravel websocket

第 1 步。

  • 创建 websocket conf 文件以在后台运行代码会像

  • /etc/supervisor/conf.d (Debian/Ubuntu) 或 /etc/supervisord.d (Red Hat/CentOS) 创建文件websockets.conf

  • 然后输入这段代码

[program:websockets]
command=/usr/bin/php <laravel-path>/artisan websockets:serve
numprocs=1
autostart=true
autorestart=true
user=<username>
  • 然后运行supervisorctl updatesupervisorctl start websockets

在这一步中,您的 laravel websocket 将在端口 6001 的后台启动

第 2 步。

  • 创建像socket.example.com这样的子域
  • 现在通过nginxnginx 配置代码连接到此域到端口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;
  }
}

在此设置中,您的子域已准备好接受 websocket 请求但没有 ssl

第 3 步。

  • 安装certbot 以获得该域的免费 ssl
  • 一旦安装certbot,只需运行sudo certbot,然后你会提示选择一个域来获取ssl

在此步骤中,您为子域获取 ssl 现在您已准备好接受此端口上的wss 连接

第 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,
        ]
    ],
],

在这个设置中,我们将 websocket 连接到子域,这里的端口是空的,因为子域是端口 6001 的代理

您可以在此处验证您的wss 是否在https://www.websocket.org/echo.html 中工作,只需复制&lt;subdomain&gt;/app/broadcasting?protocol=7&amp;client=js&amp;version=7.0.3&amp;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

【讨论】:

  • enabledTransports: ['ws', 'wss'] 取消注释此行并确保 socket.elitewagers.proxweb.eu 在此域中您已安装 ssl
  • Error in connection establishment: net::ERR_CERT_COMMON_NAME_INVALID 这个错误我遇到了这个wss 你可以在websocket.org/echo.html 使用这个url wss://socket.elitewagers.proxweb.eu/app/broadcasting?protocol=7&amp;client=js&amp;version=7.0.3&amp;flash=false 验证这个
  • 确保在端口 6001 上运行 websocket 然后通过 nginx 或 apache2 代理到 socket.elitewagers.proxweb.eu 然后它会工作
  • @Kamelsh Paul 我看到这个答案是针对 nginx 如果还提供如何在 Apache 2 服务器的 centos vps 中配置 Laravel Websockets 会更有帮助。
  • @JoneySpark 你可以查看如何在 apache 中做代理 stackoverflow.com/questions/8541182/…
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多