I'm using latest https://github.com/pydanny/cookiecutter-django template for my app and I want to use nginx instead of caddy webserver for production. So my docker compose is the same as default: https://github.com/pydanny/cookiecutter-django/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/production.yml
except I switch caddy to nginx:
nginx:
build:
context: .
dockerfile: ./compose/production/nginx/Dockerfile
image: abs_production_nginx
depends_on:
- django
ports:
- "0.0.0.0:80:80"
env_file:
- ./.envs/.production/.nginx
My nginx docker file:
FROM nginx:latest
ADD ./compose/production/nginx/nginx.conf /etc/nginx/nginx.conf
And configuration:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream app {
server django:5000;
}
server {
listen 80;
charset utf-8;
location / {
try_files $uri @proxy_to_app;
}
# cookiecutter-django app
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
}
}
This config works, but I can't serve static files. I tried to add
location /media {
autoindex on;
alias /app/media;
}
location /static {
autoindex on;
alias /app/staticfiles;
}
And in docker compose add:
volumes_from:
- django
Tried to copy whole project to nginx container, that didn't work, because I do collectstatic
command inside django
container