How to fix warning: "Application evaluated unhealthy due to redirection." on Azure Health Check Feature

Viewed 552

I ran "Diagnose and solve problems" on Azure dashboard inside one of the app services, and then I got this critical risk alert: "Application evaluated unhealthy due to redirection.".

Recommended actions is:

If the application has HTTP to HTTPS redirectoin, consider one of the following solutions.

a. Enable 'HTTPs Only' from the TLS/SSL settings blade for the respective App Service. This will force health check pings to over 443.

b. Modify the Application logic/ad a URL Rewrite rule so that requests from the user agents – ReadyForRequest/1.0+(HealthCheck) & HealthCheck/1.0 are not redirected.

I already enable 'HTTPs Only' as suggested on point (a), but I don't know how to do point (b). How to modify the Application logic/ad a URL Rewrite rule so that requests from the user agents – ReadyForRequest/1.0+(HealthCheck) & HealthCheck/1.0 are not redirected ?

Currently, I enable Health Check and set the Health Check path to /.

Thanks before for any help.

enter image description here

1 Answers

I download offical sample, and make some changes in code, hope it useful to you.

Sample Code --- .net core 3.X(HealthChecksSample)

Method 1.

Because the code is in .net core, I first recommend configuring rewrite in Startup.cs.

public void Configure(IApplicationBuilder app)
{
    app.UseHealthChecks("/");

    app.UseRouting();

    app.MapWhen(
        ctx => ctx.Request.Path.StartsWithSegments("/"),
        appBuilder =>
        {
            appBuilder.UseMiddleware<HealthCheckMiddleware>();
        }
    );

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHealthChecks("/");

        endpoints.MapGet("/{**path}", async context =>
        {
            await context.Response.WriteAsync(
                "Navigate to /health to see the health status.");
        });
    });
}

It works for me, both in local and azure web app.

enter image description here

I will add Method 2 in your another post.

Related