Home > Blockchain >  Application Initialization for an App Service in Azure not working
Application Initialization for an App Service in Azure not working

Time:05-19

We have an appservice in Azure configured to have max 8 instances and everytime we deploy, we see restart activity under Availability and Performance (Diagnostics).

We have also observed loads of 5xx errors this is happening. Our analysis so far is that requests are getting routed to cold instances which have just been spun up and these are reasons for getting failures.

I have found this guide -> https://azure.github.io/AppService/2020/05/15/Robust-Apps-for-the-cloud.html and following the Application Initialization advice.

As a result, I have added

<applicationInitialization >
  <add initializationPage="/healthcheck"/>
</applicationInitialization>

to web.config

I restarted the app service and sent few test requests to the app. In the Application Insights I can see the health endpoint being called - so application initialization logic is kicking in. However, it is calling http://localhost/healthcheck and 307 being returned.

I looked into 307 and the reason, it is returning 307 as app service is configured to only run using https protocol but http://localhost is non-https and hence service is redirecting.

What do i do need to so it calls the app service with https protocol.

I tried adding the full app url in the application initialization block but then I can see

http://localhost/https://app-service-name.azurewebsites.net/healthcheck being called - which is even worse.

What am I doing wrong?

CodePudding user response:

The warm-up module sends the request using HTTP and not with HTTPS. This behavior is by design. The workaround suggested will allow HTTP requests to localhost from warm-up module but it redirects to the HTTPS for the rest of the requests so by design here means that the warm-up module makes requests over HTTP.

To work around this limitation, you may consider enabling HTTP (unchecked the Require SSL setting under IIS Manager > SSL Settings) and use a URL Rewrite rule to redirect HTTP requests to HTTPS with the exception of the request coming from the warm-up module:

<rewrite>
    <rules>
        <rule name="No redirect on warmup request (request from localhost with warmup user agent)"
        stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="localhost" />
                <add input="{HTTP_USER_AGENT}" pattern="Initialization" />
            </conditions>
            <action type="Rewrite" url="{URL}" />
        </rule>
        <rule name="HTTP to HTTPS redirect for all requests" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
    </rules>
</rewrite>

The above is what I see in the official Microsoft documentation. Please refer to this.

Really hope it can help you!

CodePudding user response:

I assume your application runs ASP.NET Core. As mentioned in the other answer, application initialization only supports HTTP, so you need to make that work. What we did is this:

/// </summary>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...

    // IIS App initialization requires HTTP
    app.UseWhen(
        context => !WarmupController.IsWarmupRoute(context.Request.Path),
        mainApp => mainApp.UseHttpsRedirection()
    );

WarmupController.IsWarmupRoute basically only contains a StartsWith check.

This way you allow HTTP requests only for your warmup/healthcheck route.

  • Related