Nginx - 使用 try_files 时如何为 index.html 添加标头

use*_*547 7 nginx cache-control http-headers

我有以下配置(对于 Angular 应用程序):

location / {
    try_files $uri /index.html;
    add_header "Cache-Control" "no-cache" ;
}
Run Code Online (Sandbox Code Playgroud)

现在我想仅为index.html,但不为任何其他文件添加该标头。怎么做?

Mat*_*aur 5

\n

使用 \xe2\x80\x9c=\xe2\x80\x9d 修饰符可以定义 URI\n 和位置的精确匹配。如果找到完全匹配,则搜索终止。

\n
\n\n

所以你可以使用这个配置:

\n\n
location =/index.html {\n      add_header "Cache-Control" "no-cache" ;\n}\nlocation / {\n    rewrite ^(.*)  /index.html  break;\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

您可以在以下位置找到更多信息

\n\n

休息和最后不同

\n\n

带有“=”前缀的指令

\n