Adding an NGINX location block for cache exception, but it's giving me a 404 error.

Posted September 16, 2020 1.6k views

I have a file called loader.js that I don’t want to cache. So, below is the block of code I have implemented. This is on Django-One-Click app.

# to prevent the caching of loader.js
location ~* loader\.js$ {
    add_header Cache-Control "no-store";

I ran sudo nginx -t, it came out successful and restarted nginx. But, once I add the block, loader.js becomes 404. What am I doing wrong?


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Location still need to return the file itself.

location ~* loader\.js$ {
    add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
    if_modified_since off;
    expires off;
    root /your/site/folder/with/this/file/;

This will serve /loader.js, /whatever/loader.js so be careful. If you call loader always from root, you can use location = /loader.js