我们有一个设置,我们使用 Spring Boot 3.1.5 部署了
springdoc-openapi-starter-webmvc-ui
。 Spring Boot 服务器在端口 8080 上运行。我们设置了 nginx
,以便所有以 /api
开头的请求都转发到 Spring Boot:
location ~ ^/api(/|$) {
rewrite ^/api(/|$)(.*)$ /$2 break;
proxy_pass http://127.0.0.1:8080; # Forward requests to backend server
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # per https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-starter-webmvc-ui-behind-a-reverse-proxy
proxy_set_header X-Forwarded-proto https; # per https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-starter-webmvc-ui-behind-a-reverse-proxy
proxy_set_header X-Forwarded-Prefix "/api"; # per https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-starter-webmvc-ui-behind-a-reverse-proxy
}
最后 3 行是我们从 this 链接收集到的内容。在我们的
application.properties
中我们设置了:
server.forward-headers-strategy=NATIVE
server.use-forward-headers=true
我们的期望是,通过此设置,我们应该在
/api/swagger-ui/index.html
处看到 swagger UI。然而,当我们导航到该 URL 时,我们会看到以下内容:
并且浏览器中的开发人员工具显示它尝试向
/v3/api-docs
发出请求以获取配置,而它应该向 /api/v3/api-docs
发出请求 - 我们测试了此 URL 的工作原理。 Swagger UI 向后端发出的所有请求都需要带有 /api
前缀。这是我们的核心问题。
我们该如何解决这个问题?
当我们的服务器位于代理之后时,我们遇到了此类问题,我们在 application.properties 文件中添加了以下配置。
server:
forward-headers-strategy: framework