I have a django app on DigitalOcean droplet running using gunicorn and nginx. The site is working but the static files are giveing 404. Below is my ‘/etc/nginx/sites-available/django-project’ file:
server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name aakar.in;
root /usr/share/nginx/html;
index index.html index.htm;
#access_log off;
client_max_body_size 4G;
#server_name _;
keepalive_timeout 5;
location /media {
alias /home/django/django_project/django_project/media;
}
location /static/ {
alias /home/django/django_project/django_project/static;
}
location / {
proxy_pass http://127.0.0.1:9000;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM N$
}
}
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!
Heya,
To anyone stumbling upon this, here is a full revisited answer.
To configure Nginx to serve static files and media files for a Django web application, you’ll need to make sure your Django project is correctly configured for serving static and media files, and then set up Nginx to handle these files efficiently. Here are the steps to achieve this:
settings.py
file), make sure you have the following configurations set correctly:# settings.py
# Define the URL path for static files (CSS, JavaScript, etc.).
STATIC_URL = '/static/'
# Define the absolute filesystem path to the directory that will hold your static files.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Define the URL path for media files (user-uploaded files).
MEDIA_URL = '/media/'
# Define the absolute filesystem path to the directory where media files will be uploaded.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Ensure that you’ve also added the following line to your project’s urls.py
to serve media files during development:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
sudo apt-get update
sudo apt-get install nginx
/etc/nginx/sites-available/
directory. Replace your_project_name
with a meaningful name for your project:sudo nano /etc/nginx/sites-available/your_project_name
server {
listen 80;
server_name your_domain.com; # Replace with your domain or IP address
location /static/ {
alias /path/to/your/static/root; # Replace with the actual path to your STATIC_ROOT
}
location /media/ {
alias /path/to/your/media/root; # Replace with the actual path to your MEDIA_ROOT
}
location / {
include proxy_params;
proxy_pass http://unix:/path/to/your/project.sock; # Replace with your project's Unix socket
}
location /favicon.ico {
alias /path/to/your/static/root/favicon.ico; # Replace with the path to your favicon
}
location /robots.txt {
alias /path/to/your/static/root/robots.txt; # Replace with the path to your robots.txt file
}
error_page 500 502 503 504 /static/500.html;
}
Save the file and exit the text editor.
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
www-data
on Ubuntu) has read access to your static and media directories. You can adjust permissions with the following commands:sudo chown -R www-data:www-data /path/to/your/static/root
sudo chown -R www-data:www-data /path/to/your/media/root
That’s it! Nginx should now be correctly configured to serve your Django project’s static and media files. Make sure to replace placeholders like your_domain.com
, /path/to/your/static/root
, /path/to/your/media/root
, and /path/to/your/project.sock
with your actual values.
As long as the path you have listed is correct this should work. I would recommend checking to ensure that the files and directory will allow the www-data user (which nginx is running as) is able to view files in that location. You may need to adjust permissions or ownership (especially if you uploaded them as root or this home directory is not set up to allow access by non-root users other than the django user).
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.