How to make Django serve static files with Gunicorn?
When in development mode and when you are using some other server for local development add this to your url.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
More info here
When in production you never, ever put gunicorn in front. Instead you use
a server like nginx which dispatches requests to a pool of gunicorn workers and also serves the static files.
See here
Static files are not found (gunicorn)
I think your nginx declaration causes the issue.
Could you please try this:
location /static/ {
# static files
autoindex on;
autoindex_exact_size off;
# /data/atsi_webapp/ATSi_WebApp <-- may be in your case
root /exact/path/to/project/folder;
}
Instead of this:
location /static/ {
alias /data/atsi_webapp/ATSi_WebApp/static;
}
Why gunicorn cannot find static files?
From Deploying static files in the Django documentation, you must run the collectstatic
command in addition to setting the STATIC_ROOT
setting.
First make sure that you're STATIC_ROOT is set to the correct path that matches your nginx config:
STATIC_ROOT = '/home/django/innovindex/pubmed/static/'
Note that this is an absolute path.
Then run:
python manage.py collectstatic
in your project directory.
This will copy all of your static files into /home/django/innovindex/pubmed/static/
Django Gunicorn not load static files
Gunicorn will only serve the dynamic content, i.e. the Django files. So you need to setup a proxy server such as nginx to handle the static content (your CSS files). I assume you are starting Gunicorn the right way, so you just need to configure nginx to serve the static files. You can use a configuration like the following, where you just need to change the path to your static files:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8000;
}
location /static {
autoindex on;
alias /path/to/staticfiles;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Even if this configuration is setup, you have to call "./manage.py collectstatic" to make your css work
nginx django gunicorn 404 static file not loading
I didn't mention that I am using DDNS and port-forwarding my Linux machine to get access from the outside internet.
and I am also new to Nginx I didn't know that I had to add the site domain name at server_name
.
I added my site domain to server_name
and now it works properly.
It is similar to ALLOWED_HOSTS
in Django.
Thanks to everyone who helped me.
The code that worked
server {
listen 80;
server_name localhost 127.0.0.1 MYSITE_DOMAIN;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ngx/pid;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Django doesn't serve static files with NGINX + GUNICORN
NGINX + Gunicorn + Django
Django project:
djangoapp
- ...
- database
- djangoapp
- settings.py
- urls.py
- ...
- media
- static
- manage.py
- requirements.txt
Server: install venv, requirements.txt:
sudo apt-get update
sudo apt-get install -y git python3-dev python3-venv python3-pip supervisor nginx vim libpq-dev
--> cd djangoapp
pathon3 -m venv venv
source venv/bin/activate
(venv) pip3 install -r requirements.txt
Server: install NGINX:
sudo apt-get install nginx
sudo vim /etc/nginx/sites-enabled/default
Server: NGINX config:
server {
listen 80 default_server;
listen [::]:80 default_server;
location /static/ {
alias /home/ubuntu/djangoapp/static/;
}
location /media/ {
alias /home/ubuntu/djangoapp/media/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
add_header P3P 'CP="ALL DSP COR PSAa OUR NOR ONL UNI COM NAV"';
add_header Access-Control-Allow-Origin *;
}
}
Server: setup supervisor:
cd /etc/supervisor/conf.d/
sudo vim djangoapp.conf
Server: supervisor config:
[program:djangoapp]
command = /home/ubuntu/djangoapp/venv/bin/gunicorn djangoapp.wsgi -b 127.0.0.1:8000 -w 4 --timeout 90
autostart=true
autorestart=true
directory=/home/ubuntu/djangoapp
stderr_logfile=/var/log/game_muster.err.log
stdout_logfile=/var/log/game_muster.out.log
Server: update supervisor with the new process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart djangoapp
Django + nginx + gunicorn not being able to serve static files
You want 'alias' not 'root':
location /static/ {
alias /home/ubuntu/some/folder/static/;
}
Gunicorn with Django giving a problem with static files
You need to run python manage.py collectstatic
.
On your settings.py
I recommend you to use whitenoise
to serve your files.
1) pip install whitenoise
2) Add STATICFILES_STORAGE
on settings.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
3) Add to your MIDDLEWARE
on settings.py
`MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
...
]
Related Topics
Convert a Timedelta to Days, Hours and Minutes
Writing String to a File on a New Line Every Time
Force Python to Forego Native SQLite3 and Use the (Installed) Latest SQLite3 Version
Running Windows Shell Commands with Python
How to Split a Multi-Line String into Multiple Lines
What Makes Sets Faster Than Lists
Python: Binding Socket: "Address Already in Use"
Is It Ok to Use Dashes in Python Files When Trying to Import Them
Saving Interactive Matplotlib Figures
Download Returned Zip File from Url
Python Pandas: Convert Rows as Column Headers
Is Module _File_ Attribute Absolute or Relative