I've been working on serving a Django app from an Ubuntu server. I've followed all of the instructions in http://senko.net/en/django-nginx-gunicorn/, but when I get to the gunicorn_django -b 0.0.0.0:8000
step, the site suddenly stops serving static files. The site works just fine using the dev server python manage.py runserver 0.0.0.0:8000
.
I haven't changed the stock settings for anything. Any ideas why this is not working?
EDIT:
After following the rest of the tutorial and the advice of Andrew Gorcester, I added a
location /static {
root /path/to/static/files;
}
to my nginx sites-available
file, and everything seems to be working!
Does Gunicorn serve static files? Your application will now serve static assets directly from Gunicorn in production.
You can directly store a static file in the uWSGI cache during startup using the option --load-file-in-cache <filename> (you can specify it multiple times). The content of the file will be stored under the key <filename>.
Uvicorn doesn't seem to support static file serving, so you might need something else too (see e.g. https://www.uvicorn.org/deployment/#running-behind-nginx).
Gunicorn is not a general-purpose web server, all it does is serve an application (django in this case). And django does not serve static files except in development, for the convenience of the developer, because it is not an efficient or necessarily secure vehicle for serving static files.
If you follow the instructions all the way through you will be directed to set up nginx running on port 80, which will 1) proxy your application from port 8000 to port 80 and 2) serve static files on the same port, choosing which to do per request based on the URL.
It is not cause for alarm that static files do not work on port 8000 -- under this configuration they should only work on port 80, once nginx is properly configured. There are other possible configurations for django with other strategies for serving static files, although most of them follow the recommended convention of serving static files totally separate from the application like in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With