Report this

What is the reason for this report?

Django 404 with static files when caching enabled in production.

Posted on September 22, 2019

I have used this tutorial to enable caching on my Django project.

But the issue is nginx only serving media files (with caching enabled) and it throws 404 erros with static files. My nginx configuration file looks like this:

server {
    server_name _;

    root /home/x/my_project/django_project;

    access_log off;

    location /static/ {
        alias  /home/x/my_project/django_project/staticfiles/;  
    }

    location /media/ {
        alias /home/x/my_project/django_project/media/;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }  

    # location block to cache static files and media
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
            expires 7d;
    }
}

If I remove the location ~* block. Everything work back to normal.



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.

Hello,

For caching static files, I would suggest to try this way, for your /static location add the following:

    location /static/ {
        alias  /home/x/my_project/django_project/staticfiles/;  
        expires 7d;
    }

Hope that this helps! Regards, Bobby

The issue arises because the location ~* block is matching both static and media file requests, and since it comes after the /static/ and /media/ blocks, it overrides those specific configurations.

To fix this, you need to ensure that the location ~* block does not interfere with the /static/ and /media/ blocks. This can be achieved by ensuring more specific location blocks (like /static/ and /media/) are checked first, and only unmatched requests fall through to the ~* block.

Here’s the corrected configuration:

server {
    server_name _;

    root /home/x/my_project/django_project;

    access_log off;

    location /static/ {
        alias /home/x/my_project/django_project/staticfiles/;
        expires 7d;
        add_header Cache-Control "public";
    }

    location /media/ {
        alias /home/x/my_project/django_project/media/;
        expires 7d;
        add_header Cache-Control "public";
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }

    # Generic cache location for other static file types
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
        expires 7d;
        add_header Cache-Control "public";
    }
}

Key Changes:

  1. Moved caching rules directly into /static/ and /media/ blocks:

    • Ensures these specific paths handle caching explicitly.
    • Prevents interference from the ~* block.
  2. Preserved the location ~* block:

    • It handles caching for other static file types not covered by /static/ and /media/.
  3. Added Cache-Control headers:

    • Encourages browsers to cache the resources effectively.

This configuration ensures that /static/ and /media/ files are served correctly while applying caching to relevant file types without overriding the more specific blocks.

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.