首页 > 解决方案 > nginx 代理背后的 SpringDoc/Swagger

问题描述

我们在代理后面运行服务,nginx以便:

http://service-post:8080/swagger-ui.html被路由到公共地址https://host.com/services/post/swagger-ui.html

或者从另一种方式定义:

当 nginx 收到 on 请求时https://host.com/services/post/swagger-ui.html,它会剥离/services/post/前缀并将请求传递给路径上的post服务。/swagger-ui.html

在设置任何东西(使用默认 SpringDoc 配置)之前,我可以正确地看到http://service-post:8080/swagger-ui.html.

要在 host.com 上设置公共地址的路径,我使用:

springdoc.api-docs.path:        /services/post/api-docs
springdoc.swagger-ui.path:      /services/post/swagger-ui.html
springdoc.swagger-ui.configUrl: /services/post/v3/api-docs/swagger-config

然而,这似乎完全阻止了它:

/swagger-ui.html,/api-docs/v3/api-docs/swagger-config返回404forservice-post:8080/*https://host.com/services/post/*

似乎唯一可行的是https://host.com/services/post/swagger-ui/index.html,它显示了 petstore 文档。

我们没有使用 Spring Boot ,只使用5.3.1版本的 Spring MVC 。

那么如何设置以保持对原始路径(例如/api-docs)的处理,但在前缀路径()上执行查找/services/post/api-docs

标签: swaggerswagger-uispringdocspringdoc-openapi-ui

解决方案


最后我完全忽略了默认重定向:

  • swagger-ui.html-> `swagger-ui/index.html?url=/v3/api-docs

并实现了我自己的:

  • docs->swagger-ui/index.html?url=MY_PREFIX/v3/api-docs

这样我就不需要更改任何东西,一切都使用默认设置。


推荐阅读