AI 应用开发,不就是调个接口么?
苑斗剂墩项目上使用SpringCloudGateway作为网关承接公网上各个业务线进来的请求流量在网关的前面有两台Nginx反向代理了网关网关做了一系列的前置处理后转发请求到后面各个业务线的服务简要的网络链路为网关域名(wmg.test.com) - ... - Nginx -F5(硬负载域名fp.wmg.test) - 网关 - 业务系统某天负责运维Nginx的团队要增加两台新的Nginx机器原因说来话长按下不表使用两台新的Nginx机器替代掉原先反向代理网关的两台Nginx。SRE等级定性P1一个月黑风高的夜晚负责运维Nginx的团队进行了生产变更在两台新机器上部署了Nginx然后让网络团队将网关域名的流量切换到了两台新的Nginx机器上刚切换完立马有业务线团队的人反应过网关的接口请求都变成400了。负责运维Nginx的团队又让网络团队将网关域名流量切回到原有的两台Nginx上业务线过网关的接口请求恢复正常持续了两分多钟SRE等级定性P1。负责运维Nginx的团队说两台新的Nginx配置和原有的两台Nginx配置一样看不出什么问题找到我让我从网关排查有没有什么错误日志。不太可能吧如果新的两台Nginx配置和原有的两台Nginx配置一样的话不会出现请求都是400的问题啊我心想不过还是去看了网关上的日志在那个时间段网关没有错误日志出现。看了下新Nginx的日志Options请求正常返回204其它的GET、POST请求都是400Options是预检请求在Nginx层面就处理返回了新Nginx的日志示例如下10.x.x.x:63048 - 10.x.x.x:8099 [2025-07-17T10:36:2608:00] 10.x.x.x:8099 OPTIONS /api/xxx HTTP/1.1 204 0 https://domain/ Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 - [req_time:0.000 s] [upstream_connect_time:- s] [upstream_header_time:- s] [upstream_resp_time:- s] [-]10.x.x.x:63048 - 10.x.x.x:8099 [2025-07-17T10:36:2608:00] 10.x.x.x:8099 POST /api/xxx HTTP/1.1 400 0 https://domain/ Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 - [req_time:0.001 s] [upstream_connect_time:0.000 s] [upstream_header_time:0.001 s] [upstream_resp_time:0.001 s] [10.x.x.x:8082]去找了网络团队从流量回溯设备上看到400确实是网关返回的还没有到后面的业务系统400代表BadRequest我怀疑是不是请求体的问题想让网络将那个时间段的流量包数据取下来分析网络没给只给我了业务报文参数走网关请求的业务参数报文是加密的我本地运行程序可以正常解密报文我反馈给了负责运维Nginx的团队。负责运维Nginx的团队又花了一段时间定位问题还是没有头绪又找到我让我帮忙分析调查下。介入调查我说测试环境地址是啥我先在测试环境看下能不能复现负责运维Nginx的团队成员说没有在测试环境搭建测试这一次变更是另一个成员直接生产变更。??我要来了新的Nginx配置文件和老的Nginx配置文件比对了下发现有不一样的地方老Nginx上反向代理网关的配置如下server {listen 8080;server_name wmg.test.com;add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-Options nosniff;add_header Content-Security-Policy frame-ancestors self;location / {proxy_hide_header host;client_max_body_size 100m;add_header Access-Control-Allow-Origin $http_origin always;add_header Access-Control-Allow-Credentials true always;add_header Access-Control-Allow-Methods GET, POST, OPTIONS, DELETE, PUT;add_header Access-Control-Allow-Headers ...;if ($request_method OPTIONS) {return 204;}proxy_pass http://fp.wmg.test:8090;}}新Nginx配置如下upstream http_gateways{server fp.wmg.test:8090;keepalive 30;}server {listen 8080 backlog512;server_name wmg.test.com;add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-Options nosniff;add_header Content-Security-Policy frame-ancestors self;location / {proxy_hide_header host;proxy_http_version 1.1;proxy_set_header Connection ;client_max_body_size 100m;add_header Access-Control-Allow-Origin $http_origin always;add_header Access-Control-Allow-Credentials true always;add_header Access-Control-Allow-Methods GET, POST, OPTIONS, DELETE, PUT;add_header Access-Control-Allow-Headers ...;if ($request_method OPTIONS) {return 204;}proxy_pass http://http_gateways;}}新Nginx代理网关的配置与原有Nginx上的配置区别在于使用upstream配置了网关的F5负载均衡地址upstream http_gateways{server fp.wmg.test:8090;keepalive 30;}设置http协议为1.1启用长连接proxy_http_version 1.1;proxy_set_header Connection ;我让负责运维Nginx的团队在测试环境的Nginx上按照新的Nginx配置模拟了生产环境Nginx10.100.8.11 监听9104端口网关10.100.22.48 监听8081端口Nginx的9104端口转发到网关的8081端口配置如下upstream http_gateways{server 10.100.22.48:8081;keepalive 30;}server {listen 9104 backlog512;server_name localhost;add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-Options nosniff;add_header Content-Security-Policy frame-ancestors self;location / {proxy_hide_header host;proxy_http_version 1.1;proxy_set_header Connection ;client_max_body_size 100m;add_header Access-Control-Allow-Origin $http_origin always;add_header Access-Control-Allow-Credentials true always;add_header Access-Control-Allow-Methods GET, POST, OPTIONS, DELETE, PUT;add_header Access-Control-Allow-Headers ...;if ($request_method OPTIONS) {return 204;}proxy_pass http://http_gateways;}}问题复现通过Nginx请求网关到后端服务接口问题复现请求响应400curl -v -X GET http://10.100.8.11:9104/wechat-web/actuator/info去掉下面的两个配置请求正常响应200proxy_http_version 1.1;proxy_set_header Connection ;天外来锅将这个现象反馈给了负责运维Nginx的团队结果负责运维Nginx的团队查了半天说网关不支持长连接要让网关改造。??不应该啊以往网关发版的时候是滚动发版的F5上先下掉一个机器的流量停启这个机器上的网关服务然后F5上流量F5下流量的时候是有长连接存在的每次都会等个5分钟左右才能下掉一路的流量。得先放下手头的工作花点时间来证明网关是支持长连接的。在Nginx机器上通过命令行指定长连接方式访问网关请求后端服务接口wget -d --headerConnection: keepalive http://10.100.22.48:8081/wechat-web/actuator/info http://10.100.22.48:8081/wechat-web/actuator/info http://10.100.22.48:8081/wechat-web/actuator/info回车出现如下日志Setting --header (header) to Connection: keepaliveDEBUG output created by Wget 1.14 on linux-gnu.URI encoding ‘UTF-8’Converted file name info (UTF-8) - info (UTF-8)Converted file name info (UTF-8) - info (UTF-8)--2025-07-17 13:45:08-- http://10.100.22.48:8081/wechat-web/actuator/infoConnecting to 10.100.22.48:8081... connected.Created socket 3.Releasing 0x0000000000c95a90 (new refcount 0).Deleting unused 0x0000000000c95a90.---request begin---GET /wechat-web/actuator/info HTTP/1.1User-Agent: Wget/1.14 (linux-gnu)Accept: */*Host: 10.100.22.48:8081Connection: keepalive---request end---HTTP request sent, awaiting response...---response begin---HTTP/1.1 200 OKtransfer-encoding: chunkedContent-Type: application/vnd.spring-boot.actuator.v3jsonDate: Thu, 17 Jul 2025 05:25:34 GMT---response end---200 OKRegistered socket 3 for persistent reuse.Length: unspecified [application/vnd.spring-boot.actuator.v3json]Saving to: ‘info’[ ] 83 --.-K/s in 0s2025-07-17 13:45:08 (7.75 MB/s) - ‘info’ saved [83]URI encoding ‘UTF-8’Converted file name info (UTF-8) - info (UTF-8)Converted file name info (UTF-8) - info (UTF-8)--2025-07-17 13:45:08-- http://10.100.22.48:8081/wechat-web/actuator/infoReusing existing connection to 10.100.22.48:8081.Reusing fd 3.---request begin---GET /wechat-web/actuator/info HTTP/1.1User-Agent: Wget/1.14 (linux-gnu)Accept: */*Host: 10.100.22.48:8081Connection: keepalive---request end---HTTP request sent, awaiting response...---response begin---HTTP/1.1 200 OKtransfer-encoding: chunkedContent-Type: application/vnd.spring-boot.actuator.v3jsonDate: Thu, 17 Jul 2025 05:25:34 GMT---response end---200 OKLength: unspecified [application/vnd.spring-boot.actuator.v3json]Saving to: ‘info.1’[ ] 83 --.-K/s in 0s2025-07-17 13:45:08 (9.47 MB/s) - ‘info.1’ saved [83]URI encoding ‘UTF-8’Converted file name info (UTF-8) - info (UTF-8)Converted file name info (UTF-8) - info (UTF-8)--2025-07-17 13:45:08-- http://10.100.22.48:8081/wechat-web/actuator/infoReusing existing connection to 10.100.22.48:8081.Reusing fd 3.---request begin---GET /wechat-web/actuator/info HTTP/1.1User-Agent: Wget/1.14 (linux-gnu)Accept: */*Host: 10.100.22.48:8081Connection: keepalive---request end---HTTP request sent, awaiting response...---response begin---HTTP/1.1 200 OKtransfer-encoding: chunkedContent-Type: application/vnd.spring-boot.actuator.v3jsonDate: Thu, 17 Jul 2025 05:25:34 GMT---response end---200 OKLength: unspecified [application/vnd.spring-boot.actuator.v3json]Saving to: ‘info.2’[ ] 83 --.-K/s in 0s2025-07-17 13:45:08 (11.1 MB/s) - ‘info.2’ saved [83]FINISHED --2025-07-17 13:45:08--Total wall clock time: 0.1sDownloaded: 3 files, 249 in 0s (9.25 MB/s)可以看到第一个请求建立了socket 3Connection: keepalive请求成功http响应状态码为200image第二个请求重用了第一个连接socket 3Connection: keepalive请求成功http响应状态码为200image第三个请求依然重用了第一个连接socket 3Connection: keepalive请求成功http响应状态码为200image网关是支持长连接的反馈给负责运维Nginx的团队负责运维Nginx的团队又查了半天又找到我说还是得拜托我来调查解决掉这个问题。深度调查在测试环境Nginx机器10.100.8.11上使用tcpdump命令抓取与网关相关的流量包tcpdump -vv -i ens192 host 10.100.22.48 and tcp port 8081 -w /tmp/ng400.cap找到出现http响应码为400的请求可以看到流量包中的wechat-web/actuator/info请求响应为HTTP/1.1 400 Bad Request观察请求体其中一个请求头Host的值为http_gateways这引起了我的注意image查阅资料得到HTTP/1.1协议规范定义HTTP/1.1版本必须传递Host请求头- Both clients and servers MUST support the Host request-header.- A client that sends an HTTP/1.1 request MUST send a Host header.- Servers MUST report a 400 (Bad Request) error if an HTTP/1.1request does not include a Host request-header.- Servers MUST accept absolute URIs.https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.6.1.1Host的格式可以包含. 和 - 特殊符号_ 不被支持查阅Nginx的官方文档得知proxy_set_header 有两个默认配置proxy_set_header Host $proxy_host;proxy_set_header Connection close;https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header可以看出Nginx启用了HTTP/1.1协议Host如果没有指定会取$proxy_host那么使用upstream的情况下$proxy_host就是upstream的名称而此处的upstream中包含_不是合法的Host格式。HTTP/1.1规定必须传递Host的一方面原因就是为了支持单IP地址托管多域名的虚拟主机功能方便后端服务根据不同来源Host做不同的处理。Older HTTP/1.0 clients assumed a one-to-one relationship of IP addresses and servers; there was no other established mechanism for distinguishing the intended server of a request than the IP address to which that request was directed. The changes outlined above will allow the Internet, once older HTTP clients are no longer common, to support multiple Web sites from a single IP address, greatly simplifying large operational Web servers, where allocation of many IP addresses to a single host has created serious problems.那么只要遵循了HTTP/1.1协议规范的框架Tomcat、SpringCloudGateway、...在解析Host时发现Host不是合法的格式时就响应了400。本地搭建了一个测试环境debug了下网关的代码在SpringCloudGateway解析http请求类ReactorHttpHandlerAdapter中的apply方法里面可以看到解析Host失败会响应400image下面是SpringCloudGateway解析http请求类ReactorHttpHandlerAdapter中的apply方法逻辑public Mono apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {NettyDataBufferFactory bufferFactory new NettyDataBufferFactory(reactorResponse.alloc());try {ReactorServerHttpRequest request new ReactorServerHttpRequest(reactorRequest, bufferFactory);ServerHttpResponse response new ReactorServerHttpResponse(reactorResponse, bufferFactory);if (request.getMethod() HttpMethod.HEAD) {response new HttpHeadResponseDecorator(response);}return this.httpHandler.handle(request, response).doOnError(ex - logger.trace(request.getLogPrefix() Failed to complete: ex.getMessage())).doOnSuccess(aVoid - logger.trace(request.getLogPrefix() Handling completed));}catch (URISyntaxException ex) {if (logger.isDebugEnabled()) {logger.debug(Failed to get request URI: ex.getMessage());}reactorResponse.status(HttpResponseStatus.BAD_REQUEST);return Mono.empty();}}SpringCloudGateway通过debug级别日志输出这类不符合协议规范的日志生产日志级别为info因此不会打印这样异常的日志。解决方案既然HTTP/1.1协议规定必须传递Host且没有通过配置显式指定Nginx传递的Host时Nginx会有默认值那么在Nginx的配置中增加传递Host的配置覆盖默认值的逻辑查阅Nginx的文档可以通过增加下面的配置解决proxy_set_header Host $host;在测试环境Nginx9104端口代理配置中增加上面的配置再次执行请求正常响应200。image完整配置如下upstream http_gateways{server 10.100.22.48:8081;keepalive 30;}server {listen 9104 backlog512;server_name wmg.test.com;add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-Options nosniff;add_header Content-Security-Policy frame-ancestors self;location / {proxy_set_header Host $host;proxy_hide_header host;proxy_http_version 1.1;proxy_set_header Connection ;client_max_body_size 100m;add_header Access-Control-Allow-Origin $http_origin always;add_header Access-Control-Allow-Credentials true always;add_header Access-Control-Allow-Methods GET, POST, OPTIONS, DELETE, PUT;add_header Access-Control-Allow-Headers ...;if ($request_method OPTIONS) {return 204;}proxy_pass http://http_gateways;}}解决方案不止一个:可以修改upstream的名称去掉不支持的_比如更换为http-gateways、httpgateways还可以直接指定Host的值为域名domainproxy_set_header Host doamin;

相关新闻

ks控制器resyncPeriod机制定时把ks apiserver内存和cpu打得很高

ks控制器resyncPeriod机制定时把ks apiserver内存和cpu打得很高

该氐来磷1.1 简介 ControlNet是由斯坦福大学研究者张吕敏等人于2023年提出的一种AI图像生成控制技术,核心作用是让用户在保持生成图像 “创造力” 的同时,精准控制图像的结构、姿态、轮廓、深度等关键空间信息,解决了传统扩散模型&#xff08…

2026/7/5 12:55:34 阅读更多 →
C#/.NET/.NET Core技术前沿周刊 | 第  期(年.-.)

C#/.NET/.NET Core技术前沿周刊 | 第 期(年.-.)

缸灾哦旁目录 物种进化的目的 案例——背包问题 第一步 —— 初始化 第二步 — 适应度评估 第三步 — 遗传操作(进化核心:选择、交叉、变异) ∞ 循环 总结 image 遗传算法是什么? 遗传算法是一种受生物进化论(特别是自…

2026/7/4 2:01:57 阅读更多 →
医疗AI场景下算法编程的深度解析(2026新生培训讲稿)(七)

医疗AI场景下算法编程的深度解析(2026新生培训讲稿)(七)

第三部分:进阶技术与综合实践 第13章 模型可解释性在医疗中的重要性 在医疗人工智能的临床应用中,模型的可解释性往往与预测精度同等重要。一个即使准确率很高的“黑箱”模型,如果无法向临床医生解释其决策依据,也难以获得信任并真正融入临床实践。医生需要理解模型为什么…

2026/7/5 11:29:03 阅读更多 →

最新新闻

Go项目配置管理难题:基于反射的TOML自动化解析实战

Go项目配置管理难题:基于反射的TOML自动化解析实战

Go项目配置管理难题:基于反射的TOML自动化解析实战 【免费下载链接】toml TOML parser for Golang with reflection. 项目地址: https://gitcode.com/gh_mirrors/toml/toml 在Go语言项目中,配置文件管理常面临类型安全、版本兼容性和文档同步的挑…

2026/7/6 16:54:28 阅读更多 →
国家图书馆ISBN插件:3分钟实现Calibre图书信息自动化管理的终极指南

国家图书馆ISBN插件:3分钟实现Calibre图书信息自动化管理的终极指南

国家图书馆ISBN插件:3分钟实现Calibre图书信息自动化管理的终极指南 【免费下载链接】NLCISBNPlugin 基于中国国家图书馆ISBN检索的calibre的source/metadata插件。https://doiiars.com/article/NLCISBNPlugin 项目地址: https://gitcode.com/gh_mirrors/nl/NLCIS…

2026/7/6 16:50:20 阅读更多 →
如何通过浏览器扩展自动化Markdown格式转换:Copy as Markdown技术实现详解

如何通过浏览器扩展自动化Markdown格式转换:Copy as Markdown技术实现详解

如何通过浏览器扩展自动化Markdown格式转换:Copy as Markdown技术实现详解 【免费下载链接】copy-as-markdown A browser extension to copy tabs and links as Markdown 项目地址: https://gitcode.com/gh_mirrors/co/copy-as-markdown 在技术文档编写、学术…

2026/7/6 16:46:14 阅读更多 →
高效百度网盘秒传链接实战指南:5个智能文件管理技巧解析

高效百度网盘秒传链接实战指南:5个智能文件管理技巧解析

高效百度网盘秒传链接实战指南:5个智能文件管理技巧解析 【免费下载链接】baidupan-rapidupload 百度网盘秒传链接转存/生成/转换 网页工具 (全平台可用) 项目地址: https://gitcode.com/gh_mirrors/bai/baidupan-rapidupload 百度网盘秒传链接工具是一款强大…

2026/7/6 16:46:14 阅读更多 →
如何巧妙绕过Cursor试用限制:深度解析设备标识重置技术方案

如何巧妙绕过Cursor试用限制:深度解析设备标识重置技术方案

如何巧妙绕过Cursor试用限制:深度解析设备标识重置技术方案 【免费下载链接】go-cursor-help 解决Cursor在免费订阅期间出现以下提示的问题: Your request has been blocked as our system has detected suspicious activity / Youve reached your trial request li…

2026/7/6 16:44:10 阅读更多 →
STM32F207与WSEN-ISDS加速度计运动追踪系统设计

STM32F207与WSEN-ISDS加速度计运动追踪系统设计

1. 项目背景与硬件选型解析在工业自动化和消费电子领域,精确测量物体的空间运动状态一直是个关键需求。这次我选择的硬件组合是STMicroelectronics的STM32F207VGT6微控制器搭配Wrth Elektronik的WSEN-ISDS三轴加速度计(型号2536030320001)&am…

2026/7/6 16:44:10 阅读更多 →

日新闻

H2 与 MySQL 单元测试兼容性:5 个关键 SQL 语句差异与规避方案

H2 与 MySQL 单元测试兼容性:5 个关键 SQL 语句差异与规避方案

H2与MySQL单元测试兼容性:5个关键SQL语句差异与规避方案1. 单元测试中的数据库兼容性挑战在Java开发领域,单元测试是保证代码质量的重要环节。当应用涉及数据库操作时,测试环境的搭建往往成为开发者的痛点。H2数据库因其轻量级、内存模式和快…

2026/7/6 0:01:17 阅读更多 →
Windows任务栏终极清理指南:用RBTray一键隐藏窗口到系统托盘

Windows任务栏终极清理指南:用RBTray一键隐藏窗口到系统托盘

Windows任务栏终极清理指南:用RBTray一键隐藏窗口到系统托盘 【免费下载链接】rbtray A fork of RBTray from http://sourceforge.net/p/rbtray/code/. 项目地址: https://gitcode.com/gh_mirrors/rb/rbtray 你是否厌倦了Windows任务栏上密密麻麻的图标&…

2026/7/6 0:01:17 阅读更多 →
Visual C++ 运行时库一键安装终极指南:告别DLL缺失烦恼

Visual C++ 运行时库一键安装终极指南:告别DLL缺失烦恼

Visual C 运行时库一键安装终极指南:告别DLL缺失烦恼 【免费下载链接】vcredist AIO Repack for latest Microsoft Visual C Redistributable Runtimes 项目地址: https://gitcode.com/gh_mirrors/vc/vcredist 你是否曾经遇到过这样的情况:下载了…

2026/7/6 0:05:19 阅读更多 →

周新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/6 8:11:50 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/6 8:11:52 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/6 6:52:56 阅读更多 →

月新闻