Docker, nginx, django and how to serve static files

Purpose: A set of docker containers for creating a django site.

My freeze in this process is that usually nginx directly serves static files ... Based on my understanding of a good architecture using docker, you will have a container for your wsgi server (possibly gunicorn), a separate nginx container with upstream server configuration server pointing to your container for gunners. The nginx container can load balance between multiple containers for shooting.

But this implies that I have to install static django application files in the nginx container, which seems like bad practice, since the main task is really load balancing

Is it better to have three containers: nginx, gunicorn and a dedicated static server (possibly nginx or lighthttpd) for static files?

+7
django docker nginx
source share
2 answers

Regarding serving static files, your options depend on the functionality of your application. There's a very great tool called dj-static that will help you maintain static files by adding very minimal code.

The documentation is pretty simple, and all you have to do is follow these steps.

+2
source share

I found this answer from Michael Hampton : β€œThis only works if the processes are on the same host, virtual machine or container, because it is trying to connect to the same machine. When they are in different containers, this does not work.

You need to change the nginx configuration so that it uses the internal IP address of the uwsgi container. Link from message

And definitely you need to keep in mind if you have Nginx in another container, you should also install nginx.conf, specifying your statics directory file as an alias to prevent a security problem.

Hope this code works for everyone, it took me a few people to figure out how to compose Gunicorn, docker and Nginx:

 # nginx.conf upstream djangoA { server $DOCKER_CONTAINER_SERVICE:9000 max_fails=3 fail_timeout=0; # In my case looks like: web:9000 } Server { include mime.types; # The port your site will be served on listen 80; # the domain name it will serve for server_name $YOUR_SERVER_NAME;# substitute your machine IP address or FQDN charset utf-8; #Max upload size client_max_body_size 512M; # adjust to taste location /site_media { alias $DIRECTORY_STATIC_FILES/site_media;#your Django project media files have to be inside of the container have nginxs, you can copy them with volumes. expires 30d; } location / { try_files $uri @proxy_to_app; } # Finally, send all non-media requests to the Django server. location @proxy_to_app { proxy_set_header X-Real-IP $remote_addr; proxy_redirect off; proxy_set_header Host $host; proxy_pass http://djangoA; } } 

And for docker-compose:

 #production.yml version: '2' services: db: extends: file: base.yml service: db nginx: image: nginx:latest volumes: - ./nginx:/etc/nginx/conf.d/ - ./$STATIC_FILE_ROOT/site_media:/$STATIC_FILE_ROOT/site_media ports: - "80:80" depends_on: - web web: extends: file: base.yml service: web build: args: - DJANGO_ENV=production command: bash -c "python manage.py collectstatic --noinput && chmod 775 -R project/site_media/static && gunicorn project.wsgi:application" volumes: - ./$DIRECTORY_APP:/$DIRECTORY_APP ports: - "9000:9000" depends_on: - db volumes: db_data: external: true 
+2
source share

All Articles