【问题标题】:SpringDoc/Swagger behind an nginx proxynginx 代理背后的 SpringDoc/Swagger
【发布时间】:2021-03-13 10:20:13
【问题描述】:

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

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

或者换个说法:

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

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

为了在 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 均返回 404 service-post:8080/*https://host.com/services/post/*

似乎唯一有效的是https://host.com/services/post/swagger-ui/index.html,它显示了宠物商店的文档。

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

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

【问题讨论】:

  • 你为什么使用 springdoc.swagger-ui.configUrl ?
  • 当然我试过不使用它,但它没有帮助。

标签: swagger swagger-ui springdoc springdoc-openapi-ui


【解决方案1】:

【讨论】:

  • 我当然阅读了所有文档并尝试过,但在我的情况下似乎不起作用。
  • 幸运的是,源代码是开放的,您可以在这种情况下自定义它。但就维护和可扩展性而言,这似乎并不是绝对正确的选择。
【解决方案2】:

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

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

并实现了我自己的:

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

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

【讨论】:

  • 在 springdoc-openapi(也可能是 swagger)中,您可以使用属性 springdoc.swagger-ui.url 更改 application.properties 中的 url,以便访问 swagger-ui.html 将您重定向到正确的 url。跨度>
  • 我正在使用 nginx 并从 http://host/docs/swagger-ui.html 访问 swagger ui,其中 /docs 在 nginx.conf 中用于添加标题。我面临的问题是 swagger 正在向 /v3/api-docs/swagger-config 而不是 /docs/v3/api-docs/swagger-config 发出请求并返回 404。我尝试添加 springdoc.swagger-ui.url : /docs/v3/api-docs/swagger-config 在属性中,但没有运气。 .conf ` location /docs { 重写 /docs(.*) $1 break; proxy_pass host:9090; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ` 如何从 nginx 端解决这个问题?
猜你喜欢
  • 2021-01-09
  • 2011-08-29
  • 1970-01-01
  • 2018-10-05
  • 2017-08-14
  • 2022-06-23
  • 1970-01-01
  • 2019-03-14
  • 2021-04-01
相关资源
最近更新 更多