1
votes

I host an angular frontend inside an Azure App Service on Linux. Stack is Node 12. In order to enable the response headers "Cache-Control" for the static files I have added the line

<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="3.00:00:00" />

inside the Web.config file as suggested in the microsoft documentation here

The Web.config file looks like this:

<configuration>
<system.webServer>
    <handlers>
       <clear />
        <add
            name="StaticFile"
            path="*" verb="*"
            modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"
            resourceType="Either"
            requireAccess="Read" />
    </handlers>
    <staticContent>
        <mimeMap fileExtension=".json" mimeType="application/json" />
        <mimeMap fileExtension=".*" mimeType="application/octet-stream" />
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="3.00:00:00" />
    </staticContent>
    <rewrite>
            <rules>
                <rule name="redirect all requests" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="index.html" appendQueryString="true" />
                </rule>
            </rules>
     </rewrite>
</system.webServer>

Unfortunately the headers are not set correctly. Is there something wrong with the Web.config file? Does the Azure App Service on Linux have to do something with it?

1
Doris's answer should be correct. For PaaS services, we can't do more settings. If the .htaccess is modified and the attempt is invalid, the hard code can only be used to implement cache-control every time a request is sent.Jason Pan

1 Answers

2
votes

The web.config file is only for IIS configuration system, but IIS is not for Linux.

So you could not set the cache-control header in web.config file.

If you have entity instance for Linux, you could use Apache .htaccess file:

<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
</IfModule>

If you don't have entity instance for Linux, you could set the header in code:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setHeader("Expires", "0"); // Proxies.

I have tried using .htaccess file on Azure, but it did not work as Azure is a PaaS, which did not include a entity instance.