Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlwaysOn feature does not work correctly when using a rewrite rule to redirect http to https

I have an Azure PaaS instance running a Spring-Boot packaged 'war' inside a Tomcat8 container. The problem is that, after a period of in-activity, the 'java' process is killed.

enter image description here

The above image is how it should look like when the tomcat process is not killed.

The tomcat process again starts only when I again make any sort of request to the server.

If my understanding is correct, the flag 'WEBSITE_SCM_ALWAYS_ON_ENABLED' is for the instance to stay alive i.e. the 'w3wp' process and not for Tomcat.

One solution to this problem is to 'poll' my server with a simple 'health' request to keep it alive.

It would be better if there was some Azure configuration or Spring-boot configuration for its 'httpPlatformHandler' so that I can prevent it from going to sleep.

Any help is appreciated.

**** UPDATE ****

With further help from @DavidEbbo, the problem was traced to a URL 'rewrite' rule that I had for going from 'http' to 'https' urls. The 'AlwaysOn' request is on HTTP (Port 80) and thus whenever the Azure request was being hit, it was getting a redirect(301) as a response. Still awaiting a solution.

like image 525
FrailWords Avatar asked Oct 18 '22 20:10

FrailWords


1 Answers

The root of the problem is that the URL rewrite rule that you are using to redirect http to https is preventing the AlwaysOn warmup requests from reaching your Java app.

Luckily, there is a solution.

Solution #1: use an XDT transform

Use the following steps:

  • Remove the rewrite rule from your web.config
  • Instead, use Kudu Console (or FTP but that's harder) to create a file called applicationHost.xdt file under your 'site' folder (i.e. d:\home\site\applicationHost.xdt). Hint: you can type touch applicationHost.xdt in Kudu Console to create an empty file.
  • Edit that file and paste the XDT file found here under the 'Redirect http traffic to https' section.
  • Save the file and restart your site from Portal.

Note that the AlwaysOn requests will still look like 301s in the http logs, but the right thing will still happen (i.e. Java will wake up).

Solution #2: make the change directly in your web.config

Alternatively, if you don't want to use an XDT transform and want to keep everything in your web.config, you can just add the missing pieces there. Specifically, you'll need two changes (matching the XDT above):

Add this condition to your rewrite rule:

<add input="{WARMUP_REQUEST}" pattern="1" negate="true" />

Add this applicationInitialization section under system.webServer:

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

But I suggest the XDT approach as it doesn't require tricky edits, and keeps those 'messy' things separate from your code base.

Original answer

The correct way to enable Always On is to set it to On in the Azure portal. WEBSITE_SCM_ALWAYS_ON_ENABLED is not something that you can set, but it's something that Azure sets as a result of enabling Always On.

Once you enable Always On, the root of your site (i.e. /) will be hit every few minutes, which should keep it alive.

If you need additional paths to be hit, you can use applicationInitialization. More details here.

like image 154
David Ebbo Avatar answered Oct 21 '22 22:10

David Ebbo