﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-首页技术区</title><link>http://www.blogjava.net/</link><description>专注于Java技术</description><language>zh-cn</language><lastBuildDate>Fri, 18 Sep 2026 14:42:25 GMT</lastBuildDate><pubDate>Fri, 18 Sep 2026 14:42:25 GMT</pubDate><ttl>60</ttl><item><title>Nginx生产级WebSocket实时长连接负载均衡，一文搞定</title><link>http://www.blogjava.net/jb2011/archive/2026/09/08/451796.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Tue, 08 Sep 2026 02:59:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/09/08/451796.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451796.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/09/08/451796.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451796.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451796.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文作者银河技术，有修订和改动。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">WebSocket 是基于 HTTP/1.1 Upgrade 机制实现的全双工长连接通信协议，广泛应用于 IM即时通讯、实时通知、协同编辑、行情推送、游戏服务等场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">由于 WebSocket 是长连接 + 状态敏感的协议，在 Nginx 中做反向代理时，需要特别注意一些细节。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这些细节主要是：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>协议升级（Upgrade）；</li><li style="list-style: inherit;"><em>2）</em>连接保持；</li><li style="list-style: inherit;"><em>3）</em>超时控制；</li><li style="list-style: inherit;"><em>4）</em>负载均衡策略；</li><li style="list-style: inherit;"><em>5）</em>云环境 / K8s 场景下的断连问题。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>本文从基础配置、生产配置、Docker / K8s、安全、排障、性能调优，系统性讲清 WebSocket 在 Nginx 中的正确打开方式。</strong></p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161217653-1464950295.png" alt="cover-opti" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、WebSocket如何建立长连接</h1><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202607/1834368-20260728131727233-714256079.png" alt="5" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>WebSocket 通过 HTTP/1.1 完成握手：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">GET /ws HTTP/1.1</p><p style="margin: 10px auto;">Upgrade: websocket</p><p style="margin: 10px auto;">Connection: Upgrade</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一旦升级成功，连接将不再遵循 HTTP 请求-响应模型，而是长期保持的双向 TCP 通道。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>PS：</em></strong>这也是为什么 WebSocket 对 Nginx 超时 / FD / 内核参数 / LB 极度敏感。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>更多WebSocket基础资料可以继续阅读：</strong></p><ol style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3134-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket从入门到精通，半小时就够！</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-1258-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">刨根问底HTTP与WebSocket的关系(上篇)</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-1273-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">刨根问底WebSocket与Socket的关系</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3695-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">搞懂现代Web端即时通讯技术一文就够：WebSocket、socket.io、SSE</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-3098-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Web端即时通讯实践干货：如何让你的WebSocket断网重连更快速？</a></li><li style="list-style: inherit;"><a href="http://www.52im.net/thread-1341-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">理论联系实际：从零理解WebSocket的通信原理、协议格式、安全性</a></li></ol><h1>3、基础WebSocket反向代理配置</h1><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161229408-253480156.png" alt="2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">http {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;# 基础 WebSocket 代理配置</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;upstream websocket_backend {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server 192.168.1.100:8080;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server 192.168.1.101:8080;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 支持长连接</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keepalive 10;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;server {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listen 80;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server_name ws.example.com;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location /ws/ {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 核心 WebSocket 配置</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http://websocket_backend;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_http_version 1.1;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Upgrade $http_upgrade;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Connection "upgrade";</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 重要：传递原始主机头和客户端 IP</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Host $host;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Real-IP $remote_addr;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Forwarded-Proto $scheme;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 长连接超时设置</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_read_timeout 3600s;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_send_timeout 3600s;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_connect_timeout 30s;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 禁用缓冲，确保实时性</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_buffering off;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache off;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>补充说明：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>proxy_http_version 1.1：WebSocket 必须；</li><li style="list-style: inherit;"><em>2）</em>Upgrade / Connection：完成协议升级；</li><li style="list-style: inherit;"><em>3）</em>proxy_buffering off：防止消息延迟；</li><li style="list-style: inherit;"><em>4）</em>proxy_read_timeout：防止 Nginx 主动断连。</li></ul><h1>4、生产环境必须补充的 Upgrade 头处理（关键）</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>注意：</strong>这是原始配置中最容易踩坑、但极其重要的一点。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>推荐写法：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">map $http_upgrade $connection_upgrade {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;default upgrade;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;'' close;</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>然后在 location 中：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">proxy_set_header Upgrade $http_upgrade;</p><p style="margin: 10px auto;">proxy_set_header Connection $connection_upgrade;</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>为什么这样更安全？</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>非 WebSocket 请求 &#8594; Connection: close；</li><li style="list-style: inherit;"><em>2）</em>WebSocket 请求 &#8594; Connection: upgrade。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>PS：</em></strong>避免普通 HTTP 请求被错误升级。</p><h1>5、完整生产级WebSocket配置</h1><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">http {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;upstream websocket_cluster {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip_hash;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server 10.0.1.10:8080 weight=3;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server 10.0.1.11:8080 weight=2;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server 10.0.1.12:8080 backup;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 以下健康检查依赖 nginx_upstream_check_module（非官方）</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check interval=3000 rise=2 fall=3 timeout=1000 type=http;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check_http_send "GET /health HTTP/1.0\r\n\r\n";</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check_http_expect_alive http_2xx http_3xx;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;server {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listen 443 ssl http2;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server_name ws.example.com;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssl_certificate /etc/nginx/ssl/example.com.crt;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssl_certificate_key /etc/nginx/ssl/example.com.key;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssl_protocols TLSv1.2 TLSv1.3;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location /chat/ {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http://websocket_cluster;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# WebSocket 升级</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_http_version 1.1;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Upgrade $http_upgrade;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Connection $connection_upgrade;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 原始请求信息</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Host $host;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Real-IP $remote_addr;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Forwarded-Proto $scheme;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ⚠️ 长连接超时</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_read_timeout 86400s;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_send_timeout 86400s;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 实时性</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_buffering off;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 连接限制</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;limit_conn ws_conn 1000;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">limit_conn_zone $binary_remote_addr zone=ws_conn:10m;</p></blockquote><h1>6、HTTP/2 与 WebSocket 的现实情况</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>基本配置：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">listen 443 ssl http2;</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>重要认知：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>浏览器 WebSocket 仍使用 HTTP/1.1；</li><li style="list-style: inherit;"><em>2）</em>HTTP/2 WebSocket（RFC 8441）：浏览器支持有限、Nginx 支持不完整。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>PS：</em></strong>这是正常现象，不是配置错误。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>WebSocket location 必须显式：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">proxy_http_version 1.1;</p></blockquote><h1>7、Docker / K8s 场景</h1><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161239570-982573325.png" alt="3" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">map $http_upgrade $connection_upgrade {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;default upgrade;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;'' close;</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">server {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;listen 80;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;location /ws/ {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http://websocket-service:8080;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_http_version 1.1;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Upgrade $http_upgrade;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Connection $connection_upgrade;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_read_timeout 7d;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_send_timeout 7d;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_buffering off;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>K8s / 云环境的隐藏断连点：</strong></p><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161246000-411426277.png" alt="4" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>PS：</em></strong>Nginx 再怎么配都没用，必须应用层心跳</p><h1>8、WebSocket心跳保活（生产必做）</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>推荐 20~30 秒一次：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">{ "type": "ping" }</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">否则云 LB、防火墙、NAT，都会主动断你连接。</p><h1>9、负载均衡策略的选择</h1><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161251100-1403927043.png" alt="5" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">upstream websocket_backend {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;ip_hash;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;server 10.0.1.10:8080;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;server 10.0.1.11:8080;</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>实战建议：</strong></p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161256545-1737476641.png" alt="6" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>PS：</em></strong>WebSocket 本质上 不适合强依赖 LB 算法做会话保持</p><h1>10、优雅下线与灰度发布（生产增强）</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>K8s 必配：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">terminationGracePeriodSeconds: 60</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>后端逻辑：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>收到 SIGTERM；</li><li style="list-style: inherit;"><em>2）</em>停止新 WS；</li><li style="list-style: inherit;"><em>3）</em>通知客户端重连；</li><li style="list-style: inherit;"><em>4）</em>平滑下线。</li></ul><h1>11、Nginx及Linux内核参数（高并发必调）</h1><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260907161301768-1988712547.png" alt="7" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>Nginx配置如下：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">worker_processes auto;</p><p style="margin: 10px auto;">worker_rlimit_nofile 200000;</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">events {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;worker_connections 65535;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;use epoll;</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>Linux操作系统的配置：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">net.core.somaxconn = 65535</p><p style="margin: 10px auto;">net.ipv4.tcp_tw_reuse = 1</p><p style="margin: 10px auto;">net.ipv4.tcp_fin_timeout = 15</p></blockquote><h1>12、写在最后</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>指令如下：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">wscat -c ws://domain/ws</p><p style="margin: 10px auto;">ss -an | grep ESTAB | wc -l</p></blockquote><h1>13、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1] <a href="https://www.rfc-editor.org/rfc/rfc6455.txt" target="_blank" style="color: #1d58d1; text-decoration: none;">RFC6455 协议文档</a>、<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket API文档</a>、<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/EventSource" target="_blank" style="color: #1d58d1; text-decoration: none;">SSE API文档</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2] <a href="http://www.52im.net/thread-338-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门贴：史上最全Web端即时通讯技术原理详解</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3] <a href="http://www.52im.net/thread-336-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Web端即时通讯技术盘点：短轮询、Comet、Websocket、SSE</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4] <a href="http://www.52im.net/thread-1038-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">详解Web端通信方式的演进：从Ajax、JSONP 到 SSE、Websocket</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5] <a href="http://www.52im.net/thread-3555-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">网页端IM通信技术快速入门：短轮询、长轮询、SSE、WebSocket</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6] <a href="http://www.52im.net/thread-3695-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">搞懂现代Web端即时通讯技术一文就够：WebSocket、socket.io、SSE</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7] <a href="http://www.52im.net/thread-1258-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket详解（四）：刨根问底HTTP与WebSocket的关系(上篇)</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8] <a href="http://www.52im.net/thread-1273-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket详解（六）：刨根问底WebSocket与Socket的关系</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9] <a href="http://www.52im.net/thread-3098-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Web端即时通讯实践干货：如何让你的WebSocket断网重连更快速？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10] <a href="http://www.52im.net/thread-3134-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">WebSocket从入门到精通，半小时就够！</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11] <a href="http://www.52im.net/thread-1341-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">理论联系实际：从零理解WebSocket的通信原理、协议格式、安全性</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12] <a href="http://www.52im.net/thread-4748-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">浅谈网页端IM技术及相关测试方法实践（包括WebSocket性能测试）</a>》</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13] <a href="http://www.52im.net/thread-4636-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14] <a href="http://www.52im.net/thread-3065-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">零基础IM开发入门(一)：什么是IM系统？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15] <a href="http://www.52im.net/thread-4860-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">转转客服IM系统的WebSocket集群架构设计和部署方案</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16] <a href="http://www.52im.net/thread-4896-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">基于WebSocket的IM即时通信方案在H5游戏场景下的技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17] <a href="http://www.52im.net/thread-4909-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">详解AI大模型实时通信为什么选SSE，而不是WebSocket和WebRTC</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18] <a href="http://www.52im.net/thread-4918-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">都HTML5了，Web端即时通讯技术到底该用什么？一文即懂！</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（本文同步发布于： <a href="http://www.52im.net/thread-4921-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4921-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451796.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-09-08 10:59 <a href="http://www.blogjava.net/jb2011/archive/2026/09/08/451796.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>即时通讯IM离线消息该怎么存？全面盘点主流存储方案</title><link>http://www.blogjava.net/jb2011/archive/2026/08/25/451793.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Tue, 25 Aug 2026 13:02:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/08/25/451793.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451793.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/08/25/451793.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451793.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451793.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 本文作者饭后咖啡，有修订和改动。1、引言离线消息是IM系统的核心能力，当用户不在线时，消息必须可靠存储，待用户上线后完整推送。本文将对比主流的离线消息存储方案，帮你找到最适合业务场景的架构。2、离线消息的业务特征和主流存储方案概览离线消息的业务特征：方案对比总览：3、存储方案1：MySQL分库分表表结构设计：CREATE TABLE offline_message_$table (&nbsp;&n...&nbsp;&nbsp;<a href='http://www.blogjava.net/jb2011/archive/2026/08/25/451793.html'>阅读全文</a><img src ="http://www.blogjava.net/jb2011/aggbug/451793.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-08-25 21:02 <a href="http://www.blogjava.net/jb2011/archive/2026/08/25/451793.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>