【问题标题】:Dockerized Django Nginx and Postgres Issue with Nginx.conf: refused to connectNginx.conf 的 Dockerized Django Nginx 和 Postgres 问题:拒绝连接
【发布时间】:2020-06-07 11:53:14
【问题描述】:

使用 Nginx 和 Postgres 设置 docker-compose Django 应用程序是在 digitalocean 液滴上使用单独的 docker。我在推动生产时遇到问题,我相信 gunicorn 和 nginx 之间缺乏沟通。 ' 这是 docker 在我的 digitalocean droplet 上的日志:

# docker-compose -f docker-compose.yml up
/usr/local/lib/python2.7/dist-packages/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
Starting mysite_postgres_1
Recreating mysite_django_1
Recreating mysite_nginx_1
Attaching to mysite_postgres_1, mysite_django_1, mysite_nginx_1
postgres_1  | LOG:  database system was shut down at 2020-02-23 19:13:29 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started
django_1    | mkdir: cannot create directory ‘./logs/’: File exists
django_1    | ==> ./logs/gunicorn-access.log <==
django_1    | 
django_1    | ==> ./logs/gunicorn.log <==
django_1    | [2020-02-23 19:14:07 +0000] [1] [INFO] Starting gunicorn 19.9.0
django_1    | [2020-02-23 19:14:07 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
django_1    | [2020-02-23 19:14:07 +0000] [1] [INFO] Using worker: sync
django_1    | [2020-02-23 19:14:07 +0000] [12] [INFO] Booting worker with pid: 12
django_1    | [2020-02-23 19:14:07 +0000] [13] [INFO] Booting worker with pid: 13
django_1    | [2020-02-23 19:14:07 +0000] [14] [INFO] Booting worker with pid: 14
django_1    | [2020-02-23 19:14:07 +0000] [15] [INFO] Booting worker with pid: 15
django_1    | [2020-02-23 19:14:07 +0000] [16] [INFO] Booting worker with pid: 16
nginx_1     | 74.xxx.xx.xx - - [23/Feb/2020:19:14:52 +0000] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03\xD7\xB6t*\x09\xE50\x8C\x87+\x84f\xD6jD\xFDi\x07\xFE\xED'\x1D\x22?\xBD,\xA7\x90\xFC\xD2\xDF\x00 \x1C\x93\xF8q\xAF\x10Q\xA8~hd \xD5\xBD\x9E\x1D#6no\xC4\xCA8\xA5o\xC3\x18\xFF:\xEA(\xA9\x00\x22::\x13\x01\x13\x02\x13\x03\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x00" 400 157 "-" "-" "-"
nginx_1     | 74.xxx.xx.xx - - [23/Feb/2020:19:14:52 +0000] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03k\xC9\xF6_\xAF\x04$\x1B\xBDXR\x02\xBA\xDCvR\xBE\xCA\xC3DIm\xFC)JL+\xA2Z\x15\xFCk S\xC6]\xCBngR|\xFE2\xBB\xE2\xC0'\xEC\xA7<\x98\xBB\xA0\xFE]\xF9\xB6\x0Cf\x9F(\x8F\xC5\x89\x14\x00\x22\xCA\xCA\x13\x01\x13\x02\x13\x03\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x00" 400 157 "-" "-" "-"
nginx_1     | 74.xxx.xx.xx - - [23/Feb/2020:19:15:39 +0000] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03\x9AH{~\x8FH\x7F~\x1E\x92\x82\x93F\xE9X\x8A\xE6\xB9\x88>-\xD7\xF7\xCE\xE5/u\xAA\x9C\xC2\xB08 \x02\xB6\xBF#\x97m\x81\x98\xE5z8+y\xE5\xE9W\xA7?`\xCB\xD9P\xEA4xVP\x87\xD4\xB1\x8E\x87\x00\x22ZZ\x13\x01\x13\x02\x13\x03\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x00" 400 157 "-" "-" "-"
nginx_1     | 74.xxx.xx.xx - - [23/Feb/2020:19:15:39 +0000] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03\xD0\x17TH\xF7\x02#" 400 157 "-" "-" "-"

nginx.conf

server {

    listen 80;
    server_name www.removed.com removed.com;
}

server {

    listen 443 ssl;
    server_name www.removed.com removed.com;

    ssl_certificate fullchain.pem;
    ssl_certificate_key privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;

    location / {
       proxy_pass http://django:8000;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-Proto $scheme;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       client_max_body_size 5M;
   }
}

docker-compose.yml

version: '2'

volumes:
  postgres_data: {}
  postgres_backup: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    command: gunicorn mysite.wsgi:application --bind 0.0.0.0:8000 --chdir=/app --log-level=info --log-file=./logs/gunicorn.log --access-logfile=./logs/gunicorn-access.log
    depends_on:
      - postgres

    env_file:
      #- ./envs/common/django.env
      - ./env
    volumes:
      - .:/app
    expose:
      - 8000
    links:
      - postgres
    networks:
      - database1_network
      - nginx_network


  postgres:
    build:
      context: .
      dockerfile: ./compose/postgres/Dockerfile

    volumes:
      - postgres_data:/var/lib/postgresql/data
      - postgres_backup:/backups
    ports:
      - 5432:5432
    env_file:
        - ./compose/postgres/postgres.env
    networks:
      - database1_network

  nginx:
    build:
        context: .
        dockerfile: ./compose/nginx/Dockerfile
    command: [nginx-debug, -g, daemon off;]
    ports:
      - 8000:80 #Map TCP port 80 in the container to port 8000 on the Docker host.
    depends_on:
      - django
    networks:
      - nginx_network

networks:
  database1_network:
    driver: bridge
  nginx_network:
    driver: bridge

【问题讨论】:

    标签: django docker nginx gunicorn


    【解决方案1】:

    这是我问题的答案,修复了监听 80 服务器以重定向到 ssl,然后我修复了 nginx docker 容器中的端口:

    nginx.conf

    server {
        listen 80 default_server;
        server_name www.removed.com removed.com;
        return 301 https://$host$request_uri;
    }
    
    
    server {
    
    listen 443 ssl;
    server_name www.removed.com removed.com;
    
    ssl_certificate fullchain.pem;
    ssl_certificate_key privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;
    
    location / {
       proxy_pass http://django:8000;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-Proto $scheme;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       client_max_body_size 5M;
       }
    }
    

    docker-compose.yml 更改

    nginx:
        build:
            context: .
            dockerfile: ./compose/nginx/Dockerfile
        command: [nginx-debug, -g, daemon off;]
        ports:
          - 80:80 #Map TCP port 80 in the container to port 8000 on the Docker host.
          - 443:443
        depends_on:
          - django
        networks:
          - nginx_network
    

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 2015-04-30
      • 2011-04-25
      • 2021-09-15
      • 2021-06-27
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2019-08-16
      相关资源
      最近更新 更多