首页 > 解决方案 > 无法与在 EC2 上运行的 Laravel Websockets 建立连接

问题描述

我毫不怀疑这里的评论不起作用。从表面上看,它应该。不管我做了什么,我都得到了502。我的设置有点不同:

我们有一个带有 AWS 的 ec2。我们有一个私有 IP (10.0.0.1) 和一个公共 IP (52.0.0.1)。安装Laravel Websockets后,我可以启动一个 websocket 服务器(ws)php artisan websockets:serve作为 Pusher 替换。

这就是我感到困惑的地方,我应该以 ws 开头--host=10.0.0.1吗?这是我的 nginx 设置:


 # 52.0.0.1 is not needed here. I put it here to troubleshoot ws connection
 # <actual-domain-name> is the domain name ie: foobar.com
 server_name 52.0.0.1 <actual-domain-name>;

 # The usual Laravel configs
 [..]

 location /v2/api {
   # We use the public ip address here.
   # I see no mention of the private IP in this config
   # Hey, everything works
   proxy_pass http://52.0.0.1/api/;
 }

 # I need to implement web sockets
 location /v2/api/ws {
  # This is where I'm lost. Should I use 127.0.0.1, 10.0.0.1 or 52.0.0.1?
  proxy_pass    http://127.0.0.1:6001;

  # 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;
 }

使用启动 ws 服务器的命令,php artisan websockets:serve然后使用 Postman 的 websocket 部分,我提出请求:ws://<actual-domain-name>/v2/api/ws我得到 502 Bad Gateway。

无论我如何启动 ws:

# I start it manually for troublshooting
php artisan websockets:serve --host=10.0.0.1
php artisan websockets:serve --host=127.0.0.1
php artisan websockets:serve

# It wont let me start with
# php artisan websockets:serve --host=52.0.0.1

我无法建立联系。我没有提到 ssl 只是为了简单起见,所以我使用ws://而不是wss://. 我被告知6001在 AWS 中启用了端口,并且我还6001ufw.

laravel-websockets 配置会干扰使用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'),
        'useTLS' => true,
        'encrypted' => true,
        'host' => 127.0.0.1,
        'port' => 6001,
        'scheme' => 'http', // I'll use https once I get this working
        'curl_options' => [
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ],
    ],
],

标签: phplaravelwebsocketnginx-reverse-proxylaravel-websockets

解决方案


它不会让我开始php artisan websockets:serve --host=52.0.0.1

52.0.0.1是您的公共 Inet4 地址,这不在您的地址范围内。您不能在路由器上公开该服务。

如果您想进行负载平衡:如果本地机器同时运行这两种服务,那么您需要在环回本地公开它。如果另一台机器负责在同一网络上运行该服务,那么您需要将其公开给它的私有 IPV4。

以下是环回的示例(节点同时运行两个服务):

# expose the service on the loopback address
php artisan websockets:serve --host=127.0.0.1

location /v2/api/ws {
  # It also may be worth noting you'll need an X-Forwarded-For for the remote address if you plan on using the remote IPV4 inside your application
  proxy_pass    http://127.0.0.1:6001;

  # 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;
 }

请记住重新加载 Nginx 服务以使更改生效。


推荐阅读