无法与运行在EC2上的Laravel Websockets连接



我毫不怀疑这里的注释不起作用。从目前的情况来看,应该是这样。不管我做什么,我得到的都是502。我的设置有点不同:

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

这就是我困惑的地方,我应该用--host=10.0.0.1开始ws吗?下面是我的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坏网关。

无论我如何开始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://发出请求。我被告知在AWS中启用了端口6001,并且我也为ufw启用了端口6001

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

不允许我从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服务以使更改生效。

最新更新