Question

How do I get Nginx/Gunicorn to load static files from Django?

This is my first time learning how to deploy a hobby Django project to a live server, following the DigitalOcean tutorial Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 22.04. I can’t figure out the proper way to load static files.

The tutorial notes that the Django admin interface will not automatically display CSS styling. To fix this, it walks you through setting up the static directory in settings.py and /etc/nginx/sites-available/myproject so that Nginx knows where to get static content. But after completing the tutorial, I’m still left with an admin page that returns 403 errors on static content.

It seems that this tutorial never actually shows you how to give Nginx permission to get static content from /home/sammy/myprojectdir. Maybe this next step is obvious to most people, but I’m a beginner and it isn’t obvious to me. I’ve seen many suggestions online for how to give Nginx the proper permissions, but I have no idea if those suggestions are secure or follow best practices. I’m left with solutions that “work”, but I don’t know if those solutions are recommended.

For instance, I can edit /etc/nginx/nginx.conf and change user www-data; to user sammy;. That gets the static content to load, but is that insecure if sammy has sudo privileges? Alternatively, I can chmod the /home/sammy/myprojectdir/ directory multiple ways, but I lack the intuition to know if I’m doing something I shouldn’t. Some posts say to never leave static content in /home and to always move it to /var/www, and yet the DigitalOcean guide suggests that /home is appropriate. You can probably understand my confusion.

At this point, I could really use guidance on what the next step should be after completing the tutorial. What is the recommended way to give Nginx permission to load static content from Django?

TL;DR I followed this tutorial. I now have a running Django server, but the admin interface still won’t load static content. What should I do next?


Submit an answer

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!

Sign In or Sign Up to Answer

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,

Can you share the output of the following command:

ls -lah /home/sammy/myprojectdir

The static directory needs to be in there and you should also be able to see your static files if you run ls -lah /home/sammy/myprojectdir/static.

Also is the owner of the static files inside the /home/sammy/myprojectdir/static directory set to the Nginx user?

Best,

Bobby

I am facing the same problem. Just can’t get Nginx to serve the static files. Please can someone help?