Search code examples
nginxhttp-headerscache-control

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


I have the following config (for an Angular app):

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

Now I would like to add that header only for index.html, but not for any other files. How to do that?


Solution

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

    so you can use this configure :

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

    you can find more information in

    different in break and last

    Directives with the "=" prefix