Django docker无法访问Nginx端口

时间:2019-01-06 04:21:21

标签: django docker nginx

我正在码头化 Django 2.x 应用

Dockerfile

FROM python:3-alpine
RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc curl
RUN apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev postgresql-dev
RUN apk add --no-cache bash
ENV PYTHONUNBUFFERED 1
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
RUN set -ex && mkdir /app
COPY Pipfile /app
COPY Pipfile.lock /app
WORKDIR /app
RUN pip install pipenv
RUN pipenv install --system --deploy
ADD . /app/
RUN chmod +x start.sh
RUN chmod +x wait-for-it.sh
EXPOSE 9010

docker-compose.yml

version: '3'

services:
  nginx:
    image: nginx:alpine
    container_name: "originor-nginx"
    ports:
      - "10080:9010"
      - "10443:43"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./app/static_cdn/static_root:/app/static_cdn/static_root
      - originor_media_volume:/app/static_cdn/media_root
    depends_on:
      - web
    networks:
      - originor_web_network
  web:
    build: .
    container_name: "originor-web"
    command: ["./wait-for-it.sh", "db:5432", "--", "./start.sh"]
    volumes:
      - .:/app
      - ./app/static_cdn/static_root:/app/static_cdn/static_root
      - originor_media_volume:/app/static_cdn/media_root
    ports:
      - "9010:9010"
    depends_on:
      - db
    networks:
      - originor_web_network
      - originor_db_network
  db:
    image: postgres:11
    container_name: "originor-postgres-schema"
    volumes:
      - originor_database:/var/lib/postgresql/data
    networks:
      - originor_db_network
    env_file:
      - config/db/originor_database.env
    ports:
      - "5432:5432"

networks:
  originor_web_network:
    driver: bridge
  originor_db_network:
    driver: bridge

volumes:
  originor_database:
  originor_static_volume:
  originor_media_volume:

nginx.conf

error_log   /var/log/nginx/error.log;

include /etc/nginx/conf.d/proxy.conf;

upstream web {
    ip_hash;
    server web:9010 fail_timeout=0;
}

server {
    listen 10080;
    server_name localhost;
    access_log /var/log/nginx/localhost.access.log combined;

    location /static/ {
        autoindex on;
        alias /app/static_cdn/static_root/;
    }

    location /media/ {
        alias /app/static_cdn/media_root/;
    }

    location / {
        proxy_pass http://web/;
    }
}

服务器已启动使用gunicorn

gunicorn --pythonpath src originor.wsgi:application \
    --bind 0.0.0.0:9010 \
    --workers 3

因此,nginx正在监听端口 10080 ,而Django服务器正在 9010 上运行。

当我访问http://localhost:10080时,它不会加载,而访问http://localhost:9010时可以访问。

docker-compose控制台中没有关于nginx的日志。

运行命令docker-compose logs nginx仅给出

  

附加到originor-nginx

1 个答案:

答案 0 :(得分:0)

使用proxy_pass

    location / {
        proxy_pass         http://web:10080/;
        proxy_redirect     off;
        proxy_intercept_errors on;
        proxy_next_upstream error timeout http_502 http_504;
    }