You need to enable JavaScript to run this app. 如何在GCP Flex环境部署使用supervisor的Laravel6 WebSocket服务?
优惠活动
大模型
产品
解决方案
定价
更多

如何在GCP Flex环境部署使用supervisor的Laravel6 WebSocket服务?

Laravel 6 部署beyondcode/laravel-websockets到GCP Flex App Engine操作流程

前置问题排查

现有配置存在3处直接导致服务无法正常运行的错误:

  • supervisor配置中enviroment为拼写错误,正确键名为environment,会直接导致环境变量注入失败
  • app.yaml未声明自定义supervisor配置路径,GCP运行时不会加载你的进程启动规则
  • 未配置公网端口代理,Flex App Engine仅对外暴露80/443端口,本地运行的6001端口服务无法直接被公网访问

第一步:修正配置文件

1.1 修正additional-supervisord.conf

修正拼写错误、显式指定WebSocket服务的监听绑定地址,避免不同版本包默认绑定127.0.0.1导致的请求拦截,最终配置如下:

[program:websockets]
command = php %(ENV_APP_DIR)s/artisan websockets:serve --host=0.0.0.0 --port=6001
environment = PORT="6001"
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes=0
user = root
autostart = true
autorestart = true
priority = 10
stopwaitsecs = 20

1.2 新增Nginx路径代理配置

在项目根目录创建nginx-app.conf,将WebSocket相关的公网请求转发到本地运行的6001端口服务,同时配置长连接超时参数避免连接被自动掐断,配置内容:

# 转发WebSocket握手与长连接流量
location /laravel-websockets {
    proxy_pass http://127.0.0.1:6001;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_read_timeout 86400;
}
# 转发Pusher协议的接口请求
location /apps/ {
    proxy_pass http://127.0.0.1:6001;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
}

1.3 修正app.yaml

新增自定义Nginx、supervisor配置的声明,修正可能导致文件未上传的跳过规则,最终配置:

runtime: php
env: flex

runtime_config:
  document_root: public
  # 加载自定义Nginx配置
  nginx_conf: ./nginx-app.conf
  # 加载自定义supervisor进程配置
  supervisor_configs:
    - ./additional-supervisord.conf

service: orders

skip_files:
  - .env
  - .git
  - node_modules

manual_scaling:
  instances: 1

env_variables:
  APP_NAME: "Havn"
  BROADCAST_DRIVER: pusher
  PUSHER_APP_ID: SOMEID
  PUSHER_APP_KEY: SOMEKEY
  PUSHER_APP_SECRET: SOMESECRET
  PUSHER_APP_CLUSTER: mt1
  LARAVEL_WEBSOCKETS_PORT: 6001
  # 服务端内部走本地回环调用WebSocket服务,无需修改配置
  PUSHER_SCHEME: https
  PUSHER_HOST: 127.0.0.1
  PUSHER_PORT: 6001
  QUEUE_DRIVER: database

beta_settings:
  cloud_sql_instances: "INSTANCE ID"

第二步:部署前校验

  • 确认项目根目录下的additional-supervisord.conf、nginx-app.conf未被skip_files规则排除,确保部署时文件会正常上传
  • 确认安装的beyondcode/laravel-websockets版本适配Laravel 6(需使用1.x版本,3.x版本不兼容Laravel 6)
  • 检查config/websockets.php中的allowed_origins配置,允许App Engine的服务域名,避免握手时被跨域规则拦截

第三步:执行部署

在项目根目录执行部署命令,替换为实际的GCP项目ID:
gcloud app deploy app.yaml --project=你的项目ID

第四步:部署后验证

  • 打开GCP控制台对应服务的日志面板,筛选orders服务日志,确认supervisor启动的websockets进程处于RUNNING状态,无启动报错
  • 访问公网域名下的WebSocket握手路径,确认返回正常响应(非连接拒绝、502错误)
  • 测试前端WebSocket连接逻辑,确认能正常建立长连接、收发消息

内容的提问来源于stack exchange,提问作者user11779651

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.23 02:36:04