0

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

Arti
  • 7,356
  • 12
  • 57
  • 122

2 Answers2

0

After transferring your project on VPS, first use collectstatic command:

python manage.py collectstatic

this path: /app/staticfiles should be parent directory of static files not itself.

 alias /app/staticfiles;

if it's not working, use this:

 root /app/staticfiles;

after changing nginx file, restart nginx

sudo service nginx restart
Ali
  • 2,541
  • 2
  • 17
  • 31
  • Not sure what are you talking about. I have two containers. In first django container i do a collectstatic command that create a directory with static files. And how do I serve it with nginx from another container – Arti May 30 '18 at 13:11
  • Can I see settings.py file and tree directory of your project? – Ali May 31 '18 at 00:28
0

This should work in a develop environment; I used this for my cookiecutter-django app. But, full disclosure I'm new to Nginx and it's probably not the best for a prod environment.

location /static/ {
  proxy_pass http://app/static/;
}

location /media/ {
  proxy_pass http://app/media/;
}
lgants
  • 3,665
  • 3
  • 22
  • 33