Nginx - 如何在使用 try_files 时为 index.html 添加 header

Nginx - how to add header for index.html when using try_files

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

location / {
    try_files $uri /index.html;
    add_header "Cache-Control" "no-cache" ;
}

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

using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.

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

location =/index.html {
      add_header "Cache-Control" "no-cache" ;
}
location / {
    rewrite ^(.*)  /index.html  break;

}

您可以在

中找到更多信息

different in break and last

Directives with the "=" prefix