1

I'm trying to use gunicorn to serve static files before configuring nginx as reverse proxy and I got a little bit confused.

When I run my applications it seems like gunicorn is not able to find the static folder into the application.

I have the following script to run my Django application.

#!/bin/bash

# Start with development server
# echo Start server.
# python manage.py runserver 0.0.0.0:8000

# python manage.py collectstatic --noinput  # Collect static files

# # Prepare log files and start outputting logs to stdout
touch /srv/logs/gunicorn.log
touch /srv/logs/access.log
tail -n 0 -f /srv/logs/*.log &

# # Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn django_project.wsgi:application \
    --name ds4dems \
    --bind 0.0.0.0:8000 \
    --workers 3 \
    --log-level=info \
    --log-file=/srv/logs/gunicorn.log \
    --access-logfile=/srv/logs/access.log \
    "$@"

The result is that style and images are not collected from main_app static folder.

Folder structure is the following.

django_project
---- django_project
---- main_app
-------- static

Following I tried to run the same application with development server and the statics are collected. Then I rerun with Gunicorn and styles and images are served to the browser without errors.

What exactly is going on?

Does it have to do with this? Is the only available option to set nginx to let him see the files?

Community
  • 1
  • 1
sparaflAsh
  • 646
  • 1
  • 9
  • 26
  • 1
    I have a drawing which you might find useful: http://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/. It assumes you use `nginx` or `apache`, but it may be useful even if you use whitenoise, as the principles are the same. – Antonis Christofides Dec 05 '16 at 14:34
  • That's great. I will make a poster of that. :) – sparaflAsh Dec 05 '16 at 14:42

2 Answers2

3

It is possible to serve static files with gunicorn by using an extra middleware library: Whitenoise. As that documentation shows, pretty much all you need to do is include it in your MIDDLEWARE_CLASSES setting.

Note however that it is still preferable to serve the files directly via nginx, rather than going down this route.

Also note you seem confused about the difference between collecting static files and serving them. "Collecting" refers to the process of going into all your individual static directories and copying/linking the files for serving. That is the job of the manage.py collectstatic command: it has nothing to do with gunicorn or nginx, you will always need to run that command. Once you've done so, you will be able to serve them either directly or via whitenoise.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I'm definitely confused. I had to comment out `python manage.py collectstatic --noinput` as it seemed not to be able to locate the statics. Why the development server succeeds and the application don't? – sparaflAsh Dec 05 '16 at 14:20
  • 1
    Oops. I suppose I just have to set STATIC_ROOT and STATIC_URL. in settings.py to use the collectstatic application. – sparaflAsh Dec 05 '16 at 14:24
0

Gunicorn itself only serves dynamic files.

Besides Ngnix, you can also use AWS S3 and RawGit to serve the static assets.

ichbinblau
  • 4,507
  • 5
  • 23
  • 36