Warm tip: This article is reproduced from serverfault.com, please click

SpringDoc/Swagger behind an nginx proxy

发布于 2020-12-01 13:32:40

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)?

Questioner
Vojtěch
Viewed
0
Vojtěch 2020-12-02 19:40:34

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.