Kong路由配置咨询:如何实现浏览器地址从/login/test/page跳转至/endpoint/test/page
Kong路由跳转配置问题
现有配置
- name: test host: test-service port: 80 protocol: http path: /endpoint/ routes: - name: test_route strip_path: true preserve_host: true paths: - /login
现有行为
- 访问
http://localhost/login时,浏览器地址变为http://localhost/endpoint - 访问
http://localhost/login/test/page时,浏览器地址保持http://localhost/login/test/page,上游服务器能正常响应
预期需求
访问 http://localhost/login/test/page 时,浏览器最终显示 http://localhost/endpoint/test/page
你需要的是外部重定向(触发浏览器地址栏变更),而strip_path仅负责修改转发到上游的请求路径,不会引发浏览器跳转。要实现地址栏更新,需让Kong返回3xx重定向响应,具体可通过以下方式实现:
方案一:使用Kong redirect 插件
给目标路由添加redirect插件,配置路径重写规则,将/login/*映射到/endpoint/*:
# 为test_route路由添加redirect插件 curl -X POST http://localhost:8001/routes/test_route/plugins \ --data "name=redirect" \ --data "config.redirect_type=permanent" \ # 可选值:temporary(302临时重定向)、permanent(301永久重定向) --data "config.regex_uri=^/login/(.*)$" \ --data "config.replacement=/endpoint/\1"
同时调整原路由的strip_path为false,因为重定向后请求将使用新路径,无需再剥离原路由路径:
routes: - name: test_route strip_path: false preserve_host: true paths: - /login
方案二:上游服务返回重定向响应
若上游服务可修改,可让其在收到请求时返回包含Location: /endpoint/test/page的3xx响应。但此方式依赖上游配合,灵活性不如插件方案。
配置完成后,访问/login/test/page时,Kong会返回重定向响应,浏览器地址栏将自动更新为/endpoint/test/page,同时上游仍能收到正确路径的请求。
内容的提问来源于stack exchange,提问作者user1563721
相关产品推荐
相关产品推荐

