Apache APISIX 2.15版本Mocking插件配置失效求助
Apache APISIX 2.15 Mocking插件不生效,请求仍转发至上游导致502错误
环境信息
- K8s集群通过Helm部署Apache APISIX 2.15及Dashboard
- 仅修改管理员/查看者API密钥、Dashboard自定义用户名密码,其余为默认配置
- 部署命令:
helm repo add apisix https://charts.apiseven.com helm repo update # 部署APISIX helm install --set-string admin.credentials.admin="new_api_key" \ --set-string admin.credentials.viewer="new_api_key" apisix apisix/apisix --create-namespace --namespace my-apisix # 部署APISIX Dashboard,values.yaml中包含自定义用户名/密码 helm install -f values.yaml apisix-dashboard apisix/apisix-dashboard --create-namespace --namespace my-apisix
问题描述
按照官方文档配置Mocking插件时,无法调用示例中ID为1的路由,于是自定义路由并参照示例修改配置,但所有请求均返回502错误。日志显示流量被转发至不存在的服务器,推测Mocking插件未生效。
自定义路由配置:
{ "uri": "/mock-test.html", "name": "mock-sample-read", "methods": [ "GET" ], "plugins": { "mocking": { "content_type": "application/json", "delay": 1, "disable": false, "response_schema": { "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "a": { "type": "integer" }, "b": { "type": "integer" } }, "required": [ "a", "b" ], "type": "object" }, "response_status": 200, "with_mock_header": true } }, "upstream": { "nodes": [ { "host": "127.0.0.1", "port": 1980, "weight": 1 } ], "timeout": { "connect": 6, "send": 6, "read": 6 }, "type": "roundrobin", "scheme": "https", "pass_host": "node", "keepalive_pool": { "idle_timeout": 60, "requests": 1000, "size": 320 } }, "status": 1 }
APISIX日志(显示请求被转发至上游):
10.10.10.24 - - [23/Sep/2022:11:33:16 +0000] my.domain.com "GET /mock-test.html HTTP/1.1" 502 154 0.001 "-" "PostmanRuntime/7.29.2" 127.0.0.1:1980 502 0.001 "http://my.domain.com"
已知全局插件已启用(Keycloak插件可正常使用),怀疑是APISIX 2.15版本Bug,但GitHub无相关公开Issue,求可用配置示例或问题排查方向。
问题排查与解决方案
移除路由中的上游配置
Mocking插件的核心作用是直接返回模拟响应,无需配置上游节点。你当前的路由中存在upstream配置块,APISIX会优先尝试转发请求到上游,导致Mocking插件未触发。删除整个upstream配置块即可解决核心问题。简化配置验证插件有效性
先使用最简配置验证Mocking插件是否正常工作,再逐步添加复杂的响应规则:
{ "uri": "/mock-test.html", "name": "mock-sample-read", "methods": ["GET"], "plugins": { "mocking": { "content_type": "application/json", "response_status": 200, "response_example": "{\"a\":1, \"b\":2}" } }, "status": 1 }
该配置直接返回指定的JSON响应,无需依赖JSON Schema生成,更容易验证插件是否生效。
- 确认插件已加载
通过APISIX Admin API检查已加载的插件列表,确保mocking插件存在:
curl -X GET http://<apisix-admin-endpoint>/apisix/admin/plugins/list -H "X-API-KEY: new_api_key"
- 检查插件执行优先级
APISIX 2.15中Mocking插件的优先级为100,如果有其他优先级更高的插件提前终止请求流程,可能影响Mocking插件的执行。可暂时禁用其他非必要插件,验证Mocking是否正常工作。
内容的提问来源于stack exchange,提问作者namokarm
相关产品推荐
相关产品推荐

