Search code examples
asp.net-corenginxswaggernginx-reverse-proxyswashbuckle

Swashbuckle Swagger Try It Out behind NGINX reverse proxy


I'm trying to make the swaggerUI Try it out function to work behind NGINX reverse proxy. I'm using ASP.net Core with the package Swashbuckle Swagger

Since I have multiple API I added a subdomain to my nginx config.

When I use try it out it sends the request to

http://{server-ip-address}/api/myendpoint

instead of

http://{server-ip-address}/myapi/api/myendpoint

My goal is to add the myapi to the address of Try it out function. I'm not sure if I should edit my NGINX config or my ASP.net core configuration.

Thanks for your help!


Solution

  • You can try in Startup.cs:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
       ...
       app.UseSwagger(opt =>
        {
           opt.PreSerializeFilters.Add((swagger, httpReq) =>
           {
                var serverUrl = $"http://{httpReq.Host}/myapi/api/myendpoint";
                swagger.Servers = new List<OpenApiServer>{new() { Url = serverUrl }};
           });
        });