未在 azure linux 应用服务上添加缓存控制标头

问题描述

我在 Linux 上的 Azure 应用服务中托管了一个角度前端。堆栈是节点 12。 为了启用静态文件的响应头“Cache-Control”,我添加了该行

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

在微软文档中建议的 Web.config 文件here

Web.config 文件如下所示:

<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>

不幸的是,标题设置不正确。 Web.config 文件有问题吗? Linux 上的 Azure 应用服务是否必须对其进行处理?

解决方法

web.config 文件仅适用于 IIS 配置系统,但 IIS 不适用于 Linux。

因此您可以cache-control 文件中设置 web.config 标头。

如果你有 Linux 的实体实例,你可以使用 Apache .htaccess 文件:

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

如果您没有用于 Linux 的实体实例,您可以在代码中设置标题:

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

我曾尝试在 Azure 上使用 .htaccess 文件,但它不起作用,因为 Azure 是 PaaS,不包含实体实例。