Nginx代理背后的SpringDoc/Swagger

kxkpmulp  于 2024-01-07  发布在  Nginx
关注(0)|答案(3)|浏览(400)

我们在nginx代理后面运行一个服务,以便:
http://service-post:8080/swagger-ui.html路由到公共地址https://host.com/services/post/swagger-ui.html
或者从另一个Angular 来定义:
当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上设置公共地址的路径,我使用:

  1. springdoc.api-docs.path: /services/post/api-docs
  2. springdoc.swagger-ui.path: /services/post/swagger-ui.html
  3. 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,它显示了petstore文档。

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

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

uhry853o

uhry853o1#

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

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

并实现了我自己的一个:

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

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

yv5phkfx

yv5phkfx2#

我找到的解决方案没有一个对我有用,至少一开始是这样。所以这里是最终点击的完整解决方案:

  • 第一个月
  1. # === SPRINGDOC CONFIG ===
  2. server.forward-headers-strategy=framework
  3. springdoc.api-docs.enabled=true
  4. springdoc.api-docs.path=/api-docs/json
  5. springdoc.swagger-ui.path=/api-docs/ # <==== mind the trailing slash
  6. springdoc.swagger-ui.disable-swagger-default-url=true

字符串

  • 完整的nginx配置,我用docker测试了设置,并在/api/user上提供了我的API。
  1. server {
  2. server_name host.docker.internal;
  3. location /api/user/ {
  4. client_max_body_size 128M;
  5. proxy_pass http://host.docker.internal:3002/;
  6. }
  7. location /api/user/api-docs/ {
  8. rewrite /api/user/(.*) /$1 break;
  9. proxy_set_header X-Forwarded-Prefix /api/user/;
  10. proxy_pass http://host.docker.internal:3002/;
  11. }
  12. }

  • 默认子路径
  • http://localhost:3000/api-docs/json
  • http://localhost:3002/api-docs/swagger-ui/index.html
  • http://localhost:3002/api-docs/json/swagger-config
  • 反向代理路径
  • http://localhost:4000/api/user/api-docs/json
  • http://localhost:4000/api/user/api-docs/swagger-ui/index.html
  • http://localhost:4000/api/user/api-docs/json/swagger-config

这个设置的唯一问题是你不能依赖swagger的重定向。你必须写完整的URL才是安全的(如果有的话,包括尾随的斜杠)。
使用的版本:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.5.5</version>
  5. <relativePath />
  6. <!-- lookup parent from repository -->
  7. </parent>
  8. <dependency>
  9. <groupId>org.springdoc</groupId>
  10. <artifactId>springdoc-openapi-ui</artifactId>
  11. <version>1.7.0</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springdoc</groupId>
  15. <artifactId>springdoc-openapi-javadoc</artifactId>
  16. <version>1.7.0</version>
  17. </dependency>

展开查看全部
zmeyuzjn

zmeyuzjn3#

都记录在这里:

  • https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-ui-behind-a-reverse-proxy

如果你没有使用spring-boot,你可以添加ForwardedHeaderFilter bean:

相关问题