1

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

Vojtěch
  • 11,312
  • 31
  • 103
  • 173

2 Answers2

1

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.

Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • 1
    In springdoc-openapi (and swagger too maybe) you can change the url in application.properties with the property `springdoc.swagger-ui.url` so that accessing swagger-ui.html redirects you to the correct url. – Elbbard Mar 10 '21 at 13:04
  • 1
    I am using nginx & accessing swagger ui from http:// host/docs/swagger-ui.html where /docs is used in nginx.conf to add headers. The problem I am facing is swagger is making a request to /v3/api-docs/swagger-config instead of /docs/v3/api-docs/swagger-config and returns 404. I tried adding springdoc.swagger-ui.url: /docs/v3/api-docs/swagger-config in properties but no luck. .conf ` location /docs { rewrite /docs(.*) $1 break; proxy_pass http://host:9090; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ` how to fix this from nginx side? – Madan Sandiri Jan 20 '22 at 13:27
0

It's all documented here:

If you are not using spring-boot, you can add the ForwardedHeaderFilter bean:

brianbro
  • 4,141
  • 2
  • 24
  • 37
  • 1
    I of course read all that documentation and tried that, however that didn't seem to work in my case. – Vojtěch Dec 02 '20 at 11:41
  • 1
    Fortunately, the source code is open and you have the ability to customize it in this case. But this doesn't seem to be definitely the right choice in terms of maintenance and scalability. – brianbro Dec 02 '20 at 14:09