Swagger UI "Try it out" option does not work with Azure Kubernetes Nginx Ingress

Abhishek Singh 381 Reputation points
2023-06-08T12:00:23.66+00:00

Swagger UI "Try It Out" option for DotNet Core 6 API app does not work with nginx ingress in azure kubernetes service (aks) , the path for service does not get added in the URL . I have tried multiple options like adding swagger endpoint in program.cs or removing the target-rewrite option but nothing worked.

The swagger UI works fine with localhost and with LoadBalancer service but not with nginx controller as shown below:

User's image

Below is my Ingress configuration

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-world-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /serv1/(.*)
        pathType: Prefix
        backend:
          service:
            name: myapp
            port:
              number: 80
      

Below is how my "program.cs" file looks

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseSwagger();
app.UseSwaggerUI();
    
//     options =>
// {
//     options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
//     //options.RoutePrefix = "serv1/swagger";
// });


app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,939 questions
Azure Kubernetes Service (AKS)
Azure Kubernetes Service (AKS)
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
1,986 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,623 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
317 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Keith Howling 5 Reputation points Microsoft Employee
    2023-06-08T16:02:39.11+00:00

    The LB nginx ingress provisions sets its heath check to look at the / path, and the dotnet api doesnt respond at all on /! You can change the path when you install the controller as described here, that should ping it to life: https://github.com/Azure/AKS-Construction/issues/573#issuecomment-1527253720

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 61,181 Reputation points
    2023-06-12T18:20:07.24+00:00

    you have:

    app.UseHttpsRedirection()

    set, but are using http to connect from the proxy.