本文作者银河技术,即时通讯网有修订和改动。
cover-opti.png (16.4 KB, 下载次数: 0)
下载附件 保存到相册
昨天 17:01 上传
1.png (11.59 KB, 下载次数: 0)
GET /ws HTTP/1.1 Upgrade: websocket Connection: Upgrade
2.png (52.97 KB, 下载次数: 1)
http { # 基础 WebSocket 代理配置 upstream websocket_backend { server 192.168.1.100:8080; server 192.168.1.101:8080; # 支持长连接 keepalive 10; } server { listen 80; server_name ws.example.com; location /ws/ { # 核心 WebSocket 配置 proxy_pass http://websocket_backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # 重要:传递原始主机头和客户端 IP 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_read_timeout 3600s; proxy_send_timeout 3600s; proxy_connect_timeout 30s; # 禁用缓冲,确保实时性 proxy_buffering off; proxy_cache off; } } }
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade;
http { upstream websocket_cluster { ip_hash; server 10.0.1.10:8080 weight=3; server 10.0.1.11:8080 weight=2; server 10.0.1.12:8080 backup; # 以下健康检查依赖 nginx_upstream_check_module(非官方) check interval=3000 rise=2 fall=3 timeout=1000 type=http; check_http_send "GET /health HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } server { listen 443 ssl http2; server_name ws.example.com; ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; ssl_protocols TLSv1.2 TLSv1.3; location /chat/ { proxy_pass http://websocket_cluster; # WebSocket 升级 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $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_read_timeout 86400s; proxy_send_timeout 86400s; # 实时性 proxy_buffering off; # 连接限制 limit_conn ws_conn 1000; } } } limit_conn_zone $binary_remote_addr zone=ws_conn:10m;
listen 443 ssl http2;
proxy_http_version 1.1;
3.png (12.91 KB, 下载次数: 0)
昨天 17:02 上传
map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80; location /ws/ { proxy_pass http://websocket-service:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_read_timeout 7d; proxy_send_timeout 7d; proxy_buffering off; } }
4.png (9.2 KB, 下载次数: 0)
{ "type": "ping" }
5.png (57.97 KB, 下载次数: 0)
upstream websocket_backend { ip_hash; server 10.0.1.10:8080; server 10.0.1.11:8080; }
6.png (10.67 KB, 下载次数: 0)
terminationGracePeriodSeconds: 60
7.png (6.08 KB, 下载次数: 0)
worker_processes auto; worker_rlimit_nofile 200000; events { worker_connections 65535; use epoll; }
net.core.somaxconn = 65535 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 15
wscat -c ws://domain/ws ss -an | grep ESTAB | wc -l
来源:即时通讯网 - 即时通讯开发者社区!
轻量级开源移动端即时通讯框架。
快速入门 / 性能 / 指南 / 提问
轻量级Web端即时通讯框架。
详细介绍 / 精编源码 / 手册教程
移动端实时音视频框架。
详细介绍 / 性能测试 / 安装体验
基于MobileIMSDK的移动IM系统。
详细介绍 / 产品截图 / 安装体验
一套产品级Web端IM系统。
详细介绍 / 产品截图 / 演示视频
一套纯血鸿蒙NEXT产品级IM系统。
详细介绍 / 产品截图 / 安装
精华主题数超过100个。
积极发起、参与各类话题的讨论等,主题、发帖内容较有价值。
连续任职达1年以上的合格正式版主
为论区做出突出贡献的开发者、版主等。
Copyright © 2014-2026 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.125000 second(s), 36 queries , Gzip On.