问题
We are running a service behind an nginx
proxy so that:
http://service-post:8080/swagger-ui.html
is routed to public address https://host.com/services/post/swagger-ui.html
Or to define from the other way:
When nginx receives request on https://host.com/services/post/swagger-ui.html
, it strips the /services/post/
prefix and passes the request to the post
service on /swagger-ui.html
path.
Before setting up anything (with default SpringDoc configuration) I can correctly see the swagger docs on http://service-post:8080/swagger-ui.html
.
To set the paths for the public address on host.com, I am using:
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
However it seems that this brakes it completely:
/swagger-ui.html
, /api-docs
and /v3/api-docs/swagger-config
return 404
both for service-post:8080/*
and https://host.com/services/post/*
Only thing that seems to work is https://host.com/services/post/swagger-ui/index.html which shows the petstore documentation.
We are not using Spring Boot, just Spring MVC of version 5.3.1.
So how do I set up to keep the handling of the original paths (eg. /api-docs
), but performing the lookup on the prefixed path (/services/post/api-docs
)?
回答1:
It's all documented here:
- https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-ui-behind-a-reverse-proxy
If you are not using spring-boot, you can add the ForwardedHeaderFilter bean:
- https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/ForwardedHeaderFilter.html
回答2:
In the end I completely ignore the default redirect:
swagger-ui.html
-> `swagger-ui/index.html?url=/v3/api-docs
And implemented my own one:
docs
->swagger-ui/index.html?url=MY_PREFIX/v3/api-docs
This way I don't need to change anything and everything works with default settings.
来源:https://stackoverflow.com/questions/65091452/springdoc-swagger-behind-an-nginx-proxy