Report this

What is the reason for this report?

django on nginx + uwsgi (ubuntu 16.04 on digitalocean) didn't work - static files missing

Posted on September 5, 2017

I posted here already what the problem is but didn’t get an answer: https://stackoverflow.com/questions/46039302/django-on-nginx-uwsgi-static-files-dont-show

Basically there must be a mistake somewhere so that I cannot see the static files anywhere in the frontend even if “collectstatic” worked and I see the files in the terminal. I followed pretty much the digital ocean tutorial.

What can I do?



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.

Hi there,

In case taht anyone comes across this in the future, I could suggest following the steps from this tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#adjusting-the-project-settings

Mainly the ‘Adjusting the Project Settings’ step which goes over the static files configuration. As well as the ‘Configure Nginx to Proxy Pass’, so you could make sure that the Nginx configuration is also correct.

Best,

Bobby

If your Django project on Nginx + uWSGI is not serving static files correctly, despite collectstatic successfully collecting them, the issue is most likely related to Nginx’s configuration, file permissions, or how static files are referenced in your project. Here’s a systematic approach to resolving the problem:


1. Confirm Static Files Collected Successfully

After running python manage.py collectstatic, verify the files are in the correct directory specified by STATIC_ROOT in your settings.py. For example:

STATIC_ROOT = "/var/www/html/static/"
STATIC_URL = "/static/"

Check the directory:

ls /var/www/html/static/

2. Verify Nginx Configuration

Ensure your Nginx configuration properly points to the STATIC_ROOT directory. Open your Nginx configuration file (e.g., /etc/nginx/sites-available/your_project) and confirm the following:

server {
    listen 80;
    server_name yourdomain.com;

    location /static/ {
        alias /var/www/html/static/;
    }

    location /media/ {
        alias /var/www/html/media/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/path/to/your_project/your_project.sock;
    }
}
  • Replace /var/www/html/static/ with your actual STATIC_ROOT directory.
  • Restart Nginx:
sudo systemctl restart nginx

3. File Permissions

Ensure Nginx has permission to read the static files directory. Check the ownership and permissions:

sudo chown -R www-data:www-data /var/www/html/static/
sudo chmod -R 755 /var/www/html/static/

4. Debug Static File URLs

  • Visit the URL for a static file directly, e.g., http://yourdomain.com/static/css/style.css.
  • If it returns a 404 error, the issue is with the Nginx configuration or file path.

5. Debugging Nginx Logs

Check the Nginx error logs for clues:

sudo tail -f /var/log/nginx/error.log

Look for:

  • 404 errors indicating incorrect paths.
  • Permission errors indicating access issues.

6. Ensure STATIC_URL is Correct in Templates

Check your templates to ensure static files are referenced correctly using Django’s {% static %} template tag. For example:

<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">

If {% static %} is not resolving correctly:

  • Ensure django.contrib.staticfiles is in your INSTALLED_APPS.
  • Add the following in your settings.py:
from django.templatetags.static import static

Common Errors and Fixes

Error: Static Files 404 in Nginx

  • Cause: Nginx location /static/ block is misconfigured.
  • Fix: Update the alias path to match your STATIC_ROOT.

Error: Permission Denied

  • Cause: Nginx user (www-data) cannot access files.
  • Fix: Correct permissions with:
sudo chown -R www-data:www-data /var/www/html/static/
sudo chmod -R 755 /var/www/html/static/

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.