By ozwiz
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!
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";
}
}
Moved caching rules directly into /static/ and /media/ blocks:
~* block.Preserved the location ~* block:
/static/ and /media/.Added Cache-Control headers:
This configuration ensures that /static/ and /media/ files are served correctly while applying caching to relevant file types without overriding the more specific blocks.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.