【问题标题】:Django w/ Docker, Nginx, Gunicorn and SSL带有 Docker、Nginx、Gunicorn 和 SSL 的 Django
【发布时间】:2019-03-22 07:59:40
【问题描述】:

这可能是重复的,但我在以前的问题中没有找到好的答案,而且我对系统管理员和 Django 完全陌生。 Docker、Nginx 等,但我正在尝试使用来自 this guide here 的 Django 解决 HTTPS 部署问题

与我的服务器的连接不起作用,并且在浏览器中访问我的 FQDN 超时。

这是我的项目结构:

project_dir
├── assets
│   └── imgs
├── auth
│   ├── apps.py
│   ├── auth_forms.py
│   ├── __init__.py
│   ├── migrations
│   ├── __pycache__
│   ├── urls.py
│   └── views.py
├── changelog_6_3_2018.txt
├── changelog.txt
├── sub_app1
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── docker-compose.yml
├── Dockerfile
├── subapp2
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── main
│   ├── admin.py
│   ├── apps.py
│   ├── fields.py
│   ├── forms.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── __pycache__
│   ├── templatetags
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── nginx
│   ├── nginx.conf
│   └── nginx.dockerfile
├── __pycache__
│   └── wsgi.cpython-36.pyc
├── README.md
├── requirements.txt
├── sheets
│   └── LTMonServer.xlsx
├── start.sh
├── static
│   ├── admin
│   └── imgs
├── templates
│   ├── auth
│   └── fv1
├── uwsgi.ini
├── venv
│   ├── bin
│   ├── lib
│   ├── pip-selfcheck.json
│   └── share
└── wsgi.py

这是我的文件:

docker-compose.yml:

version: '2'
services:


  django:
    container_name: dsne_django
    build:
      context: .
    networks:
      - dsne-django-nginx
    volumes:
      - dsne-django-static:/usr/stc/app/static
    ports:
      - 8000:8000

  nginx:
    container_name: dsne-nginx
    build:
      context: ./nginx
      dockerfile: nginx.dockerfile
    networks:
      - dsne-django-nginx
    volumes:
      - dsne-django-static:/usr/src/app/static
      - dsne-nginx-cert:/etc/ssl/certs:ro
    ports:
      - 80:80
      - 443:443
    depends_on:
      - django

volumes:
  dsne-django-static:
  dsne-nginx-cert:

networks:
  dsne-django-nginx:
    driver: bridge

主 Dockerfile:

FROM python:3.6.4-onbuild

ENV PROJECT_ROOT /usr/src/app
COPY start.sh /start.sh

# EXPOSE port 8000
EXPOSE 8000
EXPOSE 443
# command to execute to start server
CMD ["/start.sh"]

nginx.conf:

events { worker_connections 1024; }

http {

  upstream djangocluster {
    least_conn;
    server django:80;
  }

  server {

    listen 80;
    log_subrequest on;
    return 301 https://$host$request_uri;

  }

  server {

    listen 443;
    ssl on;

    ssl_certificate     /home/johnecker/iba_ltm.pem;
    ssl_certificate_key    /home/johnecker/ibaltm.key;

    log_subrequest on;

    location / {
      proxy_pass http://djangocluster;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_cache_bypass $http_upgrade;
    }

    location /static {
        autoindex on;
        alias /usr/src/app/static;
    }
  }
}

nginx Dockerfile:

FROM nginx:1.13.3

# Copy nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Expose website on ports
EXPOSE 80
EXPOSE 443

CMD ["nginx", "-g", "daemon off;"]

start.sh:

#!/bin/bash
# Start Gunicorn processes
echo Starting Gunicorn.

#make sure we are at project root
cd $PROJECT_ROOT


python manage.py collectstatic --noinput
cd /usr/src/app
pip3 install -r requirements2.txt

pwd
sudo exec gunicorn fv1.wsgi:application -w 3 \
    --bind 0.0.0.0:80 \
    --workers 3 \

如何使此设置对 SSL/HTTPS 请求正常工作?我似乎找不到我的错误。

【问题讨论】:

    标签: django docker ssl nginx gunicorn


    【解决方案1】:

    django 监听端口 8000 而不是 80。 在 nginx 文件中替换。

    server django:8000;
    

    【讨论】:

      【解决方案2】:

      您的 gunicorn (start.sh) 尝试侦听端口 80,而 nginx 也获得了端口 80。将 gunicorn 端口更改为 8000 之类的其他端口。同时查看 nginx errors.log 文件以了解确切的错误。确保您的应用服务器 gunicorn 正在运行并接受请求。

      【讨论】:

        猜你喜欢
        • 2021-09-23
        • 1970-01-01
        • 2018-02-16
        • 2013-03-22
        • 2012-11-20
        • 2021-05-19
        • 2019-01-04
        • 2021-01-02
        • 2019-01-08
        相关资源
        最近更新 更多