django+gunicorn+nginx gives 404 while serving static files
I am trying to deploy a Django project using nginx + gunicorn + postgresql. All the configuration is done, my admin panel project static file will serve , but other static files; it returns a 404 error.(iam use run python manage.py collectstatic)
my error.log nginx :: "/blogpy/static/home/test.css" failed (2: No such file or directory)"
Structure:
blogpy
-blogpy
-config
-nginx
-nginx.conf
-docker-compose.yml
-Dockerfile
-home
-static
-home
-test.css(not working)
- requirements
-static
-templates
-.env
-docker-compose.yml
-Dockerfile
setting.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
nginx configuration:
---- nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
upstream blogpy{
server blogpy:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://blogpy;
}
location /static/ {
alias /blogpy/static/;
}
}
}
3
Answers
To get /blogpy/home/static/ files been copied into /blogpy/static/ by collectstatic command, you need to specify STATICFILES_DIRS setting
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
It is recommended to serve static files directly from
nginx
.Add the following to your nginx site config.
Try
In —- nginx.conf: