Py学习  »  Django

Django w/docker、nginx、gunicorn和ssl

Ali • 4 年前 • 896 次点击  

这可能是一个副本,但我在以前的问题中没有找到一个很好的答案,我对SysAdmin和Django完全陌生。Docker、nginx等,但我正在尝试排除使用django进行https部署的故障。 this guide here

与服务器的连接不工作,在浏览器超时时访问我的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

主要文件:

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文件:

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;"]

开始.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请求正常工作?我似乎找不到我的错误。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39131
 
896 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Zubair Alam
Reply   •   1 楼
Zubair Alam    5 年前

您的gunicorn(start.sh)试图监听端口80,nginx也获得了端口80。将Gunicorn端口更改为其他类似8000的端口。还要查看nginx errors.log文件以了解具体的错误。确保应用程序服务器Gunicorn正在运行并接受请求。

Kairat Koibagarov
Reply   •   2 楼
Kairat Koibagarov    5 年前

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

server django:8000;