Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gunicorn not serving static files

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!

like image 402
Andrew C Avatar asked Dec 19 '12 07:12

Andrew C


People also ask

Does Gunicorn serve static files?

Does Gunicorn serve static files? Your application will now serve static assets directly from Gunicorn in production.

Can uWSGI serve static files?

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>.

Can Uvicorn serve static files?

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).


1 Answers

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.

like image 163
Andrew Gorcester Avatar answered Sep 28 '22 12:09

Andrew Gorcester