﻿<?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/all</link><description>专注于Java技术</description><language>zh-cn</language><lastBuildDate>Sat, 26 Sep 2026 00:11:25 GMT</lastBuildDate><pubDate>Sat, 26 Sep 2026 00:11:25 GMT</pubDate><ttl>60</ttl><item><title>IM全文检索技术(五)：vivo敏感词匹配系统的技术实践</title><link>http://www.blogjava.net/jb2011/archive/2026/09/23/451802.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Wed, 23 Sep 2026 09:59:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/09/23/451802.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451802.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/09/23/451802.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451802.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451802.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;">本文作者vivo互联网服务器团队Liang Kangwu，有修订。</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;"><strong>编者注：</strong>本文虽不是专门的全文检索技术文章，但文中涉及到的字词匹配算法等等都是一脉相承，所以也同样收录到了&#8220;IM全文检索技术&#8221;技术专题中，希望能给你启发。</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;">谛听系统是vivo的内容审核平台，保障了vivo各互联网产品持续健康的发展。谛听支持审核多种内容类型，但日常主要审核的内容是文本，下图是一个完整的文本审核流程，包括名单匹配、敏感词匹配、AI机器审核、人工审核四个环节。待审核文本需要顺次通过名单匹配、敏感词匹配、AI机器审核三个流程，若结果为嫌疑则需要人工审核，否则将直接给出确定的结果。</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;"><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231239685-601998116.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231239685-601998116.png" lazyloaded=""  medium-zoom-image"="" 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><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;">敏感词匹配功能可以迅速地匹配文本中的敏感词汇，算法平均耗时为50ms，因其简单、快速、直接、灵活的特点，成为了审核人员对抗垃圾文本的利器。然而身处信息爆炸时代的网民们非常&#8220;优秀&#8221;，他们源源不断的发明各种新词、谐音词来绕过敏感词检测。例如某些用户会使用&#8220;啋票&#8221;、&#8220;采漂&#8221;等词汇来规避敏感词&#8220;彩票&#8221;，其中&#8220;啋票&#8221;不仅是谐音词，还包含多音字，常规的模式匹配算法很难保证完全命中。这不仅给运营规则带来挑战，也对匹配算法的精准度、漏杀率提出了要求。</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><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 alt="cover_opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231657299-1805023871.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231657299-1805023871.png" lazyloaded=""  medium-zoom-image"="" 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、系列文章</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>本文是专题系列文章中的第<em>5 </em>篇：</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-1132-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM全文检索技术(一)：微信移动端的全文检索优化之路</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-1545-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM全文检索技术(二)：微信移动端的全文检索多音字问题解决方案</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3651-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM全文检索技术(三)：网易云信Web端IM的聊天消息全文检索技术实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-3839-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM全文检索技术(四)：微信iOS端的最新全文检索技术优化实践</a>》</li><li style="list-style: inherit;">《<a href="http://www.52im.net/thread-4923-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM全文检索技术(五)：vivo敏感词匹配系统的技术实践</a>》（<span style="color: #ff0000;"><strong>* 本文</strong></span>）</li></ol><h1>3、敏感词匹配算法选型</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;">敏感词匹配功能依托于模式匹配算法。模式匹配的定义是，给定一个子串，在某个字符串中找出与该子串相同的所有子串。其中给定的子串被称为模式串，被匹配的字符串被称为目标串。基于多个模式串进行匹配的算法被称为多模式匹配算法，目前成熟的多模式匹配算法有AC自动机和WM。</p><h2>3.1 多模式匹配算法对比</h2><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 alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231245167-1824360154.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231245167-1824360154.png" lazyloaded=""  medium-zoom-image"="" 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>谛听的敏感词匹配业务有如下特点：</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>词库量大，需要维护和加载百万级别的词库（模式串）；</li><li style="list-style: inherit;"><em>2）</em>敏感词与业务特性、国家政策相关性强，无法统一约定长度、前缀等特征。</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;">WM算法对模式串的长度和前缀存在一定的要求，可能会影响业务的使用。虽然AC自动机加载耗时长，内存占用大，但敏感词加载并不频繁，且服务器内存资源充足，所以我们最终选用AC自动机作为底层算法。</p><h2>3.2 AC自动机算法介绍</h2><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;">AC自动机算法（Aho&#8211;Corasick算法）是一种字符串搜索算法，可以同时将目标串与所有模式串进行匹配，算法均摊情况下具有近似于线性的时间复杂度。</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;">AC自动机的匹配原理有两个核心概念：Trie字典树、Fail指针。下面以模式串集合{&#8220;she&#8221;, &#8220;he&#8221;, &#8220;shers&#8221;, &#8220;his&#8221;, &#8220;era&#8221;}为例，来介绍这两个概念。</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;">AC自动机算法启动时，需要将所有的模式串加载到内存中，构建成一个Trie字典树。</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;">例如下图是以模式串集合{&#8220;she&#8221;, &#8220;he&#8221;, &#8220;shers&#8221;, &#8220;his&#8221;, &#8220;era&#8221;}构建的字典树，树中每个节点代表一个字符，从根节点到某一个节点的路径，即可表示一个模式串，且红色节点表示一个字符串的终结，例如图中最右边子树上的三个节点，可代表模式串&#8220;era&#8221;。从字典树的根节点出发，可以快速的查找到某个模式串。</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;">此外，拥有相同前缀的模式串会合并到同一个子树中，例如中间子树表示模式串&#8220;he&#8221;、 &#8220;his&#8221;，这两个字符串分别是&#8220;h&#8221;节点的一个分支。AC自动机在搜索这类字符串时，可以节省匹配的次数。</p><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><p style="margin: 10px auto;"><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231251804-1768606576.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231251804-1768606576.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></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;">AC自动机在Trie树的基础上，为每个节点加入了Fail指针，上图使用虚线画出了部分节点的Fail指针，未画出虚线的节点，其Fail指针指向根节点。算法在某个节点匹配失败时，可以通过该指针转移到其他包含相同前缀的分支上继续匹配。例如匹配目标串&#8220;shis&#8221;时，对于前两个字符&#8220;sh&#8221;，Trie字典树匹配到左边字数的&#8220;h&#8221;节点上，由于该节点的子节点是字符&#8220;e&#8221;，与目标串的下一个字符&#8220;i&#8221;不匹配，因此算法通过Fail指针转移到中间子树的&#8220;h&#8221;节点上继续匹配，最终命中字符串&#8220;his&#8221;。</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;">上述的Trie字典树与Fail指针组成了AC自动机的数据模型。AC自动机匹配目标串时，会按顺序从目标串中取出字符，从Trie字典树的根节点出发，在子结点中寻找与该字符匹配的结点，若能找到，则转移到该节点，若找不到，则转移到Fail指针指向的节点。当状态转移到图中的红色节点时，就是命中了一个模式串。下图展示了AC自动机对目标串&#8220;merashisnx&#8221;进行匹配的过程。</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 alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231258751-701494437.gif" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231258751-701494437.gif" medium-zoom-image=""  lazyloaded"="" 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>4、vivo的谛听系统</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;">谛听系统基于AC自动机算法构建了一套敏感词匹配服务，将敏感词作为模式串，文本内容作为目标串，可以实现常用的中、英文敏感词匹配。但是实际的业务有很多细分的场景，普通的AC自动机算法已不能满足业务使用需求，因此我们探索了组合敏感词匹配和拼音敏感词匹配两种匹配方式，下面分别介绍。</p><h1>5、技术实践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;">常规的敏感词匹配算法通常匹配单个词或者短句，但某些词单独出现时并不违规，只有在与几个特定的词同时出现时，才能判定为违规。</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;">组合敏感词的匹配算法依然基于AC自动机算法。由于AC自动机只能判断单个词的命中情况，因此我们将组合敏感词分割成单个敏感词，并维护各敏感词与组合间的映射关系，在AC自动机算法运行结束后，只有某个组合对应的敏感词全部命中时，才能判断该组合敏感词命中。</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>为此我们需要给AC自动机添加一些前置和后置的处理步骤，具体步骤如下：</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>将组合敏感词分割为单个敏感词，并记录敏感词与组合的映射关系；</li><li style="list-style: inherit;"><em>2）</em>将分割后的组合敏感词添加到AC自动机的Tire树中；</li><li style="list-style: inherit;"><em>3）</em>运行AC自动机，匹配文本；</li><li style="list-style: inherit;"><em>4）</em>遍历匹配结果，将匹配的结果根据映射关系映射到相应的组合上；</li><li style="list-style: inherit;"><em>5）</em>记录组合的命中情况，得到最终匹配结果。</li></ul><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;"><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231304923-250651432.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231304923-250651432.png" lazyloaded=""  medium-zoom-image"="" 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><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中，算法将匹配的词映射到组合中，并标记对应的词命中。步骤5会根据各组合中单词的命中情况，来判断该组合是否命中，由于在步骤4中组合敏感词均被标记为命中，因此可判断该组合命中。</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 alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231311109-2041690379.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231311109-2041690379.png" lazyloaded=""  medium-zoom-image"="" 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>6、技术实践2：拼音敏感词</h1><h2>6.1 概述</h2><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;">在评论、弹幕等创作自由度很高的场景中，某些用户为了规避机器审核，会使用一些多音字、同音字来表达敏感词汇，例如用&#8220;啋票&#8221;来代表&#8220;彩票&#8221;等。</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;">由于多音字、同音字变化较多，将一个词的所有谐音词都穷举出来是很困难的。因此我们实现了拼音敏感词的匹配方案，将中文文本转换为拼音再匹配，通过读音匹配敏感词，即可保证命中所有的同音字，运营直接配置敏感词的拼音，例如&#8220;CAI PIAO&#8221;，即可命中&#8220;啋票&#8221;、&#8220;彩票&#8221;、&#8220;采漂&#8221;等词汇。</p><h2>6.2 匹配流程</h2><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;">常规的AC自动机算法是逐字符匹配的，因此Trie树上每个节点存储一个字符，但拼音敏感词需要按照音节匹配，因此我们将Trie树的节点数据类型由char改为String。</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><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 alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231318880-1772043676.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231318880-1772043676.png" lazyloaded=""  medium-zoom-image"="" 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;">拼音敏感词的匹配关键在于将汉字准确的转换为拼音，这一点在多音字的场景下尤为重要。由于多音字的读音是受语境影响的，现有的技术条件很难确保能将多音字准确的转换为拼音，而上文提到同音词&#8220;啋票&#8221;是用户自行造的词，算法无法准确的识别语境，可能转换得到&#8220;CAI PIAO&#8221;、&#8220;XIAO PIAO&#8221;两种不同的结果。如果拼音转换不精准，则拼音敏感词也无法准确命中。</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;">因此我们不依赖算法识别多音字的读音，而是将文本内容的所有读音都列出来匹配一遍，就可以避免避免拼音转换不精准的问题。</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;">下图展示了文本内容与拼音的对应关系，由于存在多音字，因此存储拼音的数组从一维扩展到了二维，更像是&#8220;图&#8221;的数据结构，下文将其称为拼音图。拼音图的起始节点和终止节点之间存在多条路径，这些路径对应了多音字的所有排列组合情况，为了避免漏杀，我们需要使用AC自动机将这些路径都匹配一遍。</p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><p style="margin: 10px auto;"><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231326181-398050315.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231326181-398050315.png"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></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;">从第二节的匹配流程可以看出，目标串是一维数组，因此AC自动机在匹配文本时，通常采用顺序遍历的方式。而在拼音敏感词中，由于目标串采用二维数组存储，是一种类似于&#8220;图&#8221;的数据结构，不再适合使用顺序遍历的方式，因此需要采用图的遍历算法。</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;">图的遍历算法中，最常用的就是深度优先遍历（DFS）和广度优先遍历（BFS）。由于词语是前后关联的，为了使算法更符合人类思考习惯，我们选择了DFS。DFS算法使用栈存储节点信息，在当前分支遍历完成后，通过栈中的信息回溯到上一个分支处继续遍历。由于Trie树的状态位与拼音图的节点是相关的，在DFS回溯时，Trie树也需要同步回溯，因此需要将Trie树状态位与拼音图的节点信息一起保存到DFS栈中。</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><div img-uploading-status=""  image-package-10"="" data-index="10" 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 alt="9" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231331925-831353181.gif" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231331925-831353181.gif"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><h2>6.3 终止条件与剪枝策略</h2><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;">DFS的终止条件是当所有节点都被遍历过，且算法会确保每个节点只会被遍历一次。但是DFS遍历时会在分支处回溯，所以往往终止的节点并不是待匹配文本的终点，很有可能AC自动机的匹配并未完成。</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;">例如在下图所示的匹配流程中，左图是基于待匹配文本&#8220;朱朝阳和朋友&#8221;构建的拼音图，右图是基于拼音敏感词&#8220;PENG YOU&#8221;、&#8220;ZHAO YANG&#8221;、&#8220;NI MA&#8221;、&#8220;MA DE&#8221;构建的Trie树。左图的拼音图采用DFS算法遍历，算法最后访问的节点是蓝色节点&#8220;ZHAO&#8221;，此时拼音图中所有节点均被遍历了一次，已经达到了DFS的终止条件。但右图中Trie树上的状态位处于蓝色节点&#8220;ZHAO&#8221;的位置，并没有达到终止状态，若此时停止匹配，则会导致无法命中敏感词&#8220;ZHAO YANG&#8221;，故算法应继续运行，直到AC自动机匹配失败为止。</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;">因此合适的终止条件是：拼音图所有节点均被遍历 且 AC自动机匹配失败。</p><div img-uploading-status=""  image-package-11"="" data-index="11" 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 alt="10" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231338180-818054310.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231338180-818054310.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><img alt="11" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231343633-1653593431.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231343633-1653593431.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></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;">由于算法需要结合DFS和AC自动机的状态来判断终止条件，因此会出现拼音图中一个节点和路径被遍历多次的情况，当待匹配文本中多音字数量增多时，DFS遍历的路径数量会以笛卡尔积的形式增加。</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;">而这些路径中会存在一部分重复的情况，因此在遍历的过程中需要采取合适的剪枝策略，避免搜索一些重复的路径。</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;">例如下面左图所示的遍历情况，路径&#9313;上&#8220;PENG&#8221;、&#8220; YOU&#8221;两个节点已被路径&#9312;遍历过，且对应的AC自动机状态位（参考右图）前缀并不包含当前遍历节点&#8220;HU&#8221;，所以&#8220;PENG&#8221;对应的敏感词与路径&#9313;无关，不必再搜索一次。</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>首先当前节点的下一节点已被遍历过；</li><li style="list-style: inherit;"><em>2）</em>下一节点对应的AC自动机状态与当前节点无关。</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;">我们为拼音图中的每个节点标记一个分支路径长度B，表示当前节点与上一个最近的分支节点间的路径长度，例如节点&#8220;PENG&#8221;对应的分支路径长度为B = 2（从&#8220;HU&#8221;到&#8220;PENG&#8221;），节点&#8220;YOU&#8221;的分支路径长度为B = 3（&#8220;HU&#8221;、&#8220;PENG&#8221;、&#8220;YOU&#8221;）。</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;">在AC自动机的状态机中，记录Trie树上每个节点的深度D。例如状态机中&#8220;PENG&#8221;节点的深度D=1，&#8220;YOU&#8221;节点的深度D=2。从Trie树根节点到某一个节点的路径上经过的字符连接起来，为该节点对应的模式串，因此节点的深度D即为模式串的长度。当D &lt; B时，表明当前正在匹配的模式串长度短于拼音图中当前节点的分支路径长度，所以当前的模式串与当前的路径无关。</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>拼音图中下一节点已被遍历；</li><li style="list-style: inherit;"><em>2）</em>拼音图的分支路径长度B &gt; Trie树节点的深度D。</li></ul><div img-uploading-status=""  image-package-13"="" data-index="13" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">&nbsp;</div><div img-uploading-status=""  image-package-14"="" data-index="14" 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 alt="12" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231348709-1587269741.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231348709-1587269741.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><img alt="13" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231410758-163559836.png" src="https://img2024.cnblogs.com/blog/1834368/202609/1834368-20260921231410758-163559836.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></div></div></div><h1>7、写在最后</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;">谛听系统基于AC自动机实现了普通敏感词、组合敏感词、拼音敏感词的匹配，其中组合敏感词和拼音敏感词还可以结合成为拼音组合敏感词，覆盖了大部分的文本审核场景，减轻了机审、人审的压力。</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;">另外，在政策风向发生变化时，敏感词匹配功能为运营同事提供了一种快速变更审核策略的手段，使谛听的文本审核能力更加灵活。</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;">目前谛听线上已经配置了数量超过100万的敏感词，极大程度的保障了公司的内容安全。</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>一方面我们可以采用某种方式实现&#8220;非&#8221;的逻辑，这样可以在配置敏感词时排除bad case，提升命中的精准度；</li><li style="list-style: inherit;"><em>2）</em>另一方面，我们可以实现敏感词的模糊命中，提升敏感词的命中率。</li></ul><h1>8、相关资料</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="http://www.52im.net/thread-3065-1-1.html" target="_blank" rel="noopener nofollow" 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;">[2] <a href="http://www.52im.net/thread-464-1-1.html" target="_blank" rel="noopener nofollow" 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;">[3] <a href="http://www.52im.net/thread-4378-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(七)：深度解密vivo的自研分布式ID服务(鲁班)</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-3856-1-1.html" target="_blank" rel="noopener nofollow" 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;">[5] <a href="http://www.52im.net/thread-3780-1-1.html" target="_blank" rel="noopener nofollow" 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;">[6] <a href="http://www.52im.net/thread-3887-1-1.html" target="_blank" rel="noopener nofollow" 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;">[7] <a href="http://www.52im.net/thread-4684-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">得物自研移动端弱网诊断工具的技术实践分享</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-4887-1-1.html" target="_blank" rel="noopener nofollow" 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;">[9] <a href="http://www.52im.net/thread-4905-1-1.html" target="_blank" rel="noopener nofollow" 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;">[10] <a href="http://www.52im.net/thread-3994-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">直播系统聊天技术(八)：vivo直播系统中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;">[11] <a href="http://www.52im.net/thread-4153-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">得物从0到1自研客服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;">[12] <a href="http://www.52im.net/thread-4404-1-1.html" target="_blank" rel="noopener nofollow" 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;">[13] <a href="http://www.52im.net/thread-4564-1-1.html" target="_blank" rel="noopener nofollow" 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-4627-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">陌陌技术分享：陌陌IM在后端KV缓存架构上的技术实践</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-4636-1-1.html" target="_blank" rel="noopener nofollow" 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;">[16] <a href="http://www.52im.net/thread-4690-1-1.html" target="_blank" rel="noopener nofollow" 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;">[17] <a href="http://www.52im.net/thread-4764-1-1.html" target="_blank" rel="noopener nofollow" 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;">[18] <a href="http://www.52im.net/thread-4789-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">支持百万人超大群聊的Web端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;">[19] <a href="http://www.52im.net/thread-4874-1-1.html" target="_blank" rel="noopener nofollow" 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;">[20] <a href="http://www.52im.net/thread-4886-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">B站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;">[21] <a href="http://www.52im.net/thread-4887-1-1.html" target="_blank" rel="noopener nofollow" 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;">[22] <a href="http://www.52im.net/thread-4920-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">即时通讯IM离线消息该怎么存？全面盘点主流存储方案</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-4923-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4923-1-1.html</a> ）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451802.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-23 17:59 <a href="http://www.blogjava.net/jb2011/archive/2026/09/23/451802.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>备份maven仓库</title><link>http://www.blogjava.net/wangjiaddr/archive/2026/09/20/451801.html</link><dc:creator>有机肥</dc:creator><author>有机肥</author><pubDate>Sun, 20 Sep 2026 01:13:00 GMT</pubDate><guid>http://www.blogjava.net/wangjiaddr/archive/2026/09/20/451801.html</guid><wfw:comment>http://www.blogjava.net/wangjiaddr/comments/451801.html</wfw:comment><comments>http://www.blogjava.net/wangjiaddr/archive/2026/09/20/451801.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangjiaddr/comments/commentRss/451801.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangjiaddr/services/trackbacks/451801.html</trackback:ping><description><![CDATA[<code style="box-sizing: border-box; font-variant: normal; font-stretch: 100%; line-height: 22px; font-optical-sizing: auto; font-size-adjust: none; font-kerning: auto; font-feature-settings: normal; font-variation-settings: normal; font-language-override: normal; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; background-color: #ebeef2; border-radius: 6px; align-items: center; padding: 0px 5px; display: inline-flex; color: #0f1115; font-size: 0.875em !important;"><span style="color: unset; font-family: unset; font-size: unset; font-style: unset; font-variant-ligatures: unset; font-variant-caps: unset; font-weight: unset; letter-spacing: unset; text-align: unset; text-decoration-line: unset; text-indent: unset; text-transform: unset; word-spacing: unset; -webkit-text-fill-color: unset; -webkit-text-stroke: unset; caret-color: unset; white-space: pre;"><span style="white-space: normal">https://nexus.dayu.work/repository/bp46571077-releases/</span></span>nexus3-exporter</code><span style="color: #0f1115; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; font-size: 16px; background-color: #ffffff;"> 是一个轻量的 Python 脚本，适合快速备份<br /></span>首先要安装python<br /><h2>1. 确认 pip 可用<br /><br /><span style="color: #0f1115; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; font-size: 16px; font-weight: 400; background-color: #ffffff;">先运行：</span><span style="font-size: 14px;">python -m pip --version</span><span style="color: #0f1115; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; font-size: 16px; font-weight: 400; background-color: #ffffff;">如果能看到 pip 版本号，就继续。<br /></span><span style="background-color: #ffffff; color: #0f1115; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; font-size: 22px;">2. 获取 nexus3-exporter 脚本</span></h2><h2 style="font-style: normal; font-variant: normal; font-size-adjust: none; font-language-override: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 22px; line-height: 32px; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; margin: 32px 0px 16px; color: #0f1115; background-color: #ffffff;"><span class=""><p style="margin: 16px 0px; font-size: 16px; font-weight: 400;">直接在 GitHub 搜索 <code style="box-sizing: border-box; font-variant: normal; font-stretch: 100%; line-height: 22px; font-optical-sizing: auto; font-size-adjust: none; font-kerning: auto; font-feature-settings: normal; font-variation-settings: normal; font-language-override: normal; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; background-color: #ebeef2; border-radius: 6px; align-items: center; padding: 0px 5px; display: inline-flex; font-size: 0.875em !important;">nexus3-exporter</code>，下载 ZIP 解压，然后进入解压后的目录。</p><p style="margin: 16px 0px; font-size: 16px; font-weight: 400;">进入目录后，确认里面有：</p><ul style="margin: 16px 0px; padding-left: 18px; font-size: 16px; font-weight: 400;"><li><p style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><code style="box-sizing: border-box; font-variant: normal; font-stretch: 100%; line-height: 22px; font-optical-sizing: auto; font-size-adjust: none; font-kerning: auto; font-feature-settings: normal; font-variation-settings: normal; font-language-override: normal; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; background-color: #ebeef2; border-radius: 6px; align-items: center; padding: 0px 5px; display: inline-flex; font-size: 0.875em !important;">nexus3_exporter.py</code></p></li><li style="margin-top: 6px;"><p style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><code style="box-sizing: border-box; font-variant: normal; font-stretch: 100%; line-height: 22px; font-optical-sizing: auto; font-size-adjust: none; font-kerning: auto; font-feature-settings: normal; font-variation-settings: normal; font-language-override: normal; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; background-color: #ebeef2; border-radius: 6px; align-items: center; padding: 0px 5px; display: inline-flex; font-size: 0.875em !important;">requirements.txt<br /></code></p></li></ul></span></h2><h2 style="font-style: normal; font-variant: normal; font-size-adjust: none; font-language-override: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 22px; line-height: 32px; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; margin: 32px 0px 16px; color: #0f1115; background-color: #ffffff;"><span class="">3. 安装依赖</span></h2><h2 style="font-style: normal; font-variant: normal; font-size-adjust: none; font-language-override: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 22px; line-height: 32px; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; margin: 32px 0px 16px; color: #0f1115; background-color: #ffffff;"><span class=""><ul style="margin: 16px 0px; padding-left: 18px; font-size: 16px; font-weight: 400;"><li style="margin-top: 6px;"><p style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><code style="box-sizing: border-box; font-variant: normal; font-stretch: 100%; line-height: 22px; font-optical-sizing: auto; font-size-adjust: none; font-kerning: auto; font-feature-settings: normal; font-variation-settings: normal; font-language-override: normal; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; background-color: #ebeef2; border-radius: 6px; align-items: center; padding: 0px 5px; display: inline-flex; font-size: 0.875em !important;"><span style="font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; font-size: 16px; background-color: #ffffff;">在脚本所在目录运行：</span></code></p><pre style="font-style: normal; font-variant: normal; font-size-adjust: none; font-language-override: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 13px; line-height: 22px; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; overflow: auto; white-space: pre-wrap; word-break: break-all; padding: 16px; margin: 0px !important;">python -m pip install -r requirements.txt</pre><p style="margin-right: 0px; margin-bottom: 0px; margin-left: 0px; margin-top: 0px !important;"><code style="box-sizing: border-box; font-variant: normal; font-stretch: 100%; line-height: 22px; font-optical-sizing: auto; font-size-adjust: none; font-kerning: auto; font-feature-settings: normal; font-variation-settings: normal; font-language-override: normal; font-family: Menlo, Monaco, Consolas, &quot;Cascadia Mono&quot;, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Liberation Mono&quot;, &quot;JetBrains Mono&quot;, &quot;Fira Code&quot;, Cousine, &quot;Roboto Mono&quot;, &quot;Courier New&quot;, Courier, sans-serif, system-ui; background-color: #ebeef2; border-radius: 6px; align-items: center; padding: 0px 5px; display: inline-flex; font-size: 0.875em !important;"><br /></code></p></li></ul></span></h2><h2 style="font-style: normal; font-variant: normal; font-size-adjust: none; font-language-override: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 22px; line-height: 32px; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; margin: 32px 0px 16px; color: #0f1115; background-color: #ffffff;"><span class="">4. 运行导出命令<br />要备份</span><span style="color: unset; font-family: unset; font-size: unset; font-style: unset; font-variant-ligatures: unset; font-variant-caps: unset; font-weight: unset; letter-spacing: unset; text-align: unset; text-decoration-line: unset; text-indent: unset; text-transform: unset; word-spacing: unset; -webkit-text-fill-color: unset; -webkit-text-stroke: unset; caret-color: unset; white-space: pre;">https://nexus.dayu.work/repository/bp46571077-releases/用下面的命令</span></h2><h2 style="font-style: normal; font-variant: normal; font-size-adjust: none; font-language-override: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 22px; line-height: 32px; font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; margin: 32px 0px 16px; color: #0f1115; background-color: #ffffff;"><span class=""><ul style="margin: 16px 0px; padding-left: 18px; font-size: 16px; font-weight: 400;"><li style="margin-top: 6px;"><div>python nexus3_exporter.py -u your_username -o .\bp46571077-backup -m https://nexus.dayu.work bp46571077-releases</div></li></ul></span></h2><img src ="http://www.blogjava.net/wangjiaddr/aggbug/451801.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangjiaddr/" target="_blank">有机肥</a> 2026-09-20 09:13 <a href="http://www.blogjava.net/wangjiaddr/archive/2026/09/20/451801.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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></channel></rss>