0

我正在为我的应用程序使用最新的https://github.com/pydanny/cookiecutter-django模板,并且我想使用 nginx 而不是 caddy webserver 进行生产。所以我的 docker compose 与默认相同: https ://github.com/pydanny/cookiecutter-django/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/production.yml

除了我将 caddy 切换到 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

我的 nginx 泊坞窗文件:

FROM nginx:latest

ADD ./compose/production/nginx/nginx.conf /etc/nginx/nginx.conf

和配置:

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;

    }
  }
}

此配置有效,但我无法提供静态文件。我试图添加

location /media {
    autoindex on;
    alias /app/media;
}

location /static {
    autoindex on;
    alias /app/staticfiles;
}

在 docker compose 中添加:

volumes_from:
      - django

试图将整个项目复制到 nginx 容器中,但没有成功,因为我在容器collectstatic内执行命令django

4

2 回答 2

0

这应该在开发环境中工作;我将它用于我的 cookiecutter-django 应用程序。但是,完全披露我是 Nginx 的新手,它可能不是最好的产品环境。

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

location /media/ {
  proxy_pass http://app/media/;
}
于 2018-11-25T22:34:14.740 回答
0

在 VPS 上传输您的项目后,首先使用 collectstatic 命令:

python manage.py collectstatic

此路径:/app/staticfiles 应该是静态文件的父目录,而不是它本身。

 alias /app/staticfiles;

如果它不工作,使用这个:

 root /app/staticfiles;

修改nginx文件后,重启nginx

sudo service nginx restart
于 2018-05-30T12:17:20.423 回答