Today I helped out a colleague who was struggling with the following use case:
We have an ASP.NET Core Web API with OpenAPI(Swagger) integration enabled. This ASP.NET Core Web API was running behind a reverse proxy(Yarp in case you want to know) and isn’t directly accessibel.
To explain the problem he had, let’s start from the following situation:
- The ASP.NET Core application was running on http://internalname:5000
- The YARP reverse proxy was running on https://localhost/proxy
When browsing to the Swagger endpoint on https://localhost/proxy/swagger, we got the following:
Do you notice the application url in the address bar vs the url in the Servers dropdown?
If we try to invoke a specific endpoint through the Swagger UI, Swagger tries to do the call directly to http://internalname:5000 which results in 404 error as the service is not available directly on this address.
It seems that Swagger doesn’t respect the Forwarded headers as provided through Yarp.
We fixed it by explicitly reading out the X-Forwarded-Host value and using it to fill up the servers dropdown: