使用Docker和Nginx部署Django项目

时间:2020-01-02 12:12:48

标签: django docker nginx docker-compose gunicorn

您能帮我弄清楚吗?-我正在尝试使用Docker,Gunicorn和NginX部署Django(DRF和Mezzanine CMS合并)项目的阶段版本,但是我无法从Nginx容器加载静态文件

我有一个免费域,可以用来玩游戏-http://hidro-stage.tk,但是该项目似乎只在http://hidro-stage.tk:8000/加载,没有静态文件。这使我相信Gunicorn服务器正在运行,但是Nginx部分出了问题。

这是我的docker撰写文件的样子:

```
version: '3.7'
volumes:
  static_files:
services:
  web:
    build:
      context: .
      dockerfile: docker/prod/web/Dockerfile.prod
    volumes:
      - .:/hidro
      - static_files:/static_files
    ports:
      - 8000:8000
    command: gunicorn -w 4 Asociacija.wsgi -b 0.0.0.0:8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres
    environment:
      - POSTGRES_USER=oooooo
      - POSTGRES_PASSWORD=zzzzzz
      - POSTGRES_DB=hhhhhh
    ports:
      - 5432
  nginx:
    build:
      context: .
      dockerfile: docker/prod/nginx/Dockerfile
    volumes:
      - static_files:/static_files
    ports:
      - 80:80
```

它成功构建并运行3个容器-web,db和nginx。一世 在以下入口点自动运行collectstatic命令 Web容器的Dockerfile(可能有更聪明的方法 它,以便它不会在我每次运行时收集静态文件 docker-compose up,但目前似乎可以使用)。如果我执行 nginx容器我看到它里面有所有静态文件 static_files目录。我在STATIC_URL = "/static/"中设置了STATIC_ROOT = "/static_files/"settings.py

这是我的nginx conf文件:

```
server {
    listen 80;
    server_name localhost;
    access_log  /var/log/nginx/example.log;
    server_tokens off;

    location /static/ {
        autoindex off;
        alias /static_files/;
    }

    location / {
        try_files $uri $uri/ @python_django;
    }

    location @python_django {
        proxy_pass http://web:8000;
        proxy_pass_request_headers on;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
    }
}
```

我还将51.15.192.119(测试服务器的IP)和hidro-stage.tk设置为ALLOWED_HOSTS

如果我尝试访问上述允许的主机,则会在docker-compose日志中得到此信息:

nginx_1  | 2019/12/30 16:47:57 [error] 6#6: *10 connect() failed (111: Connection refused) while connecting to upstream, client: 78.61.21.166, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://212.47.231.228:8000/favicon.ico", host: "hidro-stage.tk", referrer: "http://hidro-stage.tk/"
nginx_1  | 2019/12/30 16:53:42 [error] 6#6: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 54.202.212.187, server: localhost, request: "GET / HTTP/1.1", upstream: "http://212.47.231.228:8000/", host: "51.15.192.119"

nginx容器中的nginx日志显示:

78.61.21.166 - - [30/Dec/2019:16:47:57 +0000] "GET /favicon.ico HTTP/1.1" 502 552 "http://hidro-stage.tk/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 S$
54.202.212.187 - - [30/Dec/2019:16:53:42 +0000] "GET / HTTP/1.1" 502 552 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"

我确定这不是什么大问题,但是已经有一段时间了。 让我知道是否可以分享其他内容,以使事情变得更加清晰。

0 个答案:

没有答案