Report this

What is the reason for this report?

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

Posted on September 16, 2020

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?

Thanks.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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

Heya,

an update on an older topic

The issue you’re encountering with the loader.js file returning a 404 error after adding the new location block for cache control in Nginx is likely because the new location block does not include instructions on how to handle the request for the file beyond setting the header. As a result, Nginx does not know where to find loader.js or what to do with the request.

To fix this, you need to ensure that the location block for loader.js not only sets the appropriate headers but also processes the request correctly, typically by including a try_files directive or a root/alias directive pointing to the location of the file on the filesystem. Here’s how you can adjust your configuration:

location ~* loader\.js$ {
    add_header Cache-Control "no-store";
    try_files $uri =404;  # Adjust this line as needed
}

In this revised block:

  • add_header Cache-Control "no-store"; ensures that loader.js will not be cached.
  • try_files $uri =404; tells Nginx to look for loader.js at the specified URI and return a 404 error if it is not found. This line might need to be adjusted depending on the actual location of loader.js in your project’s directory structure.

If loader.js is not in the root directory that the rest of your server block serves files from, you might need to use the alias directive to point to its location. For example:

location ~* loader\.js$ {
    add_header Cache-Control "no-store";
    alias /path/to/directory/;  # Path to the directory containing loader.js
}

Make sure you replace /path/to/directory/ with the actual directory path where loader.js is located.

After making these changes, don’t forget to test your Nginx configuration again with sudo nginx -t and reload Nginx with sudo systemctl reload nginx.

If you continue to experience issues, you may want to review other parts of your Nginx configuration to ensure there are no conflicts or missing directives related to the handling of JavaScript files or the specific path to loader.js.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.