Hyperf 在 NginxProxyManager 如何配置 websocket?

  • 新建代理 填写域名等服务信息,选择支持WebSockets。
  • 创建 SSL
  • 编写nginx配置
bash 复制代码
location /message.io{
    proxy_pass http://<你的ip>:<对应端口号>;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;
}
  • hyperf配置
  • routes.php
bash 复制代码
// 消息ws服务器
 Router::addServer('message', function () {
     Router::get('/message.io', 'App\Controller\Web\WebSocketController', [
         'middleware' => [\App\Middleware\WsAuthMiddleware::class], // ws中间件
     ]);
 });
  • server.php配置
bash 复制代码
 [
            'name' => 'message',
            'type' => Server::SERVER_WEBSOCKET,
            'host' => '0.0.0.0',
            'port' => 9502,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
                Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
                Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
            ],
            'settings' => [
                // 心跳检测
                'heartbeat_idle_time' => 60,
                'heartbeat_check_interval' => 30,
            ],
        ],
  • 访问路径
bash 复制代码
wss://wsdev.com/message.io
相关推荐
AI小小怪21 小时前
在Linux服务器上安装CVAT (Docker 28.5.1)
运维·docker·容器·数据标注·cvat
KYGALYX1 天前
在Linux中备份msyql数据库和表的详细操作
linux·运维·数据库
lang201509281 天前
Linux高效备份:tar与gzip完全指南
linux·运维·服务器
kali-Myon1 天前
NewStarCTF2025-Week4-Web
sql·安全·web安全·php·ctf·ssti·ssrf
wanhengidc1 天前
云手机的基本原理
运维·服务器·游戏·智能手机·云计算
篙芷1 天前
两台服务器 NFS 共享目录实战
运维·服务器
Hard but lovely1 天前
linux: centos 软件包管理 yum源
linux·运维·centos
2301_816073831 天前
Chrony服务器
运维·服务器
学网络的APang1 天前
Apache HTTP Server 2.4.65 详细安装教程(基于 CentOS 7)
运维·网络
非典型代码1 天前
Jenkins发不出邮件
运维·servlet·jenkins