Spring Boot and Azure App Service: configuration tips
One of my applications runs in Azure Cloud using an App Service.
The App Service is easy to deploy (via GitHub) and monitor, but it's very limited in the configuration options.
The application hosted on Azure is public, and SEO is very important for this web app.
After running Lighthouse and other tools to identify common errors and bad practices, a couple of issues were directly linked to the deployment in Azure.
Update the HTTP Headers of the static assets
A recommendation from Google and SEO Tools is to add some cache information to the HTTP Headers to improve the loading performance of a page during future visits.
One of these options is to use Cache-Control on static assets. Usually, static assets don't change frequently and we can tell the browser to store them for e.g. a week.
We have a lot of options to add this header: proxy server (rewrite), application server, application.
Azure doesn't give us access to the server configuration and recommends rewriting the HTTP requests using their Azure Application Gateway.
We want to avoid additional layers and costs (each feature of Azure is expen$ive); therefore, we rely on the features provided by our Spring application.
Spring has the possibility to integrate a Filter
that updates every HTTP Response adding our Cache-Control
value.
This solution is perfect for pages handled by Spring, our static resources (css, javascript etc.) are handled directly by the Embedded Tomcat provided by Spring.
Spring provides a property that can be configured in application.properties
to solve the issue:
spring.web.resources.cache.cachecontrol.max-age
(from 2.6.0, in previous versions: spring.resources.cache.cachecontrol.max-age
)
Example for 604800 seconds (1 week): spring.web.resources.cache.cachecontrol.max-age: 604800
This property adds the Cache-Control
Header to our static assets automatically when they are sent to the client.
HTTP/2.0 configuration in Azure
The server was serving pages in HTTP/1.1 and not in HTTP/2.0, this was an issue according to Google Lighthouse.
By default, Azure uses HTTP/1.1 for App Service.
To enable HTTP/2.0, navigate to the 'Configuration' section of your App Service and change the HTTP Version setting from 1.1 to 2.0.
You can see the option in the following image:
Always On
Probably Azure tries to save the planet suspending automatically our application when it is not used for 20 minutes.
This is a default setting for some instances of Azure. If you are using B1, you can activate the Always On feature for free. However, this is arguably a basic feature for a web application, and despite paying Azure 100% of the time, the billing does not pause when the instance is inactive.
You can activate the functionality in the 'Configuration' section of your App Service.
