Django Nginx Docker Gunicorn 无法访问

Django Nginx Docker Gunicorn cant reach

抱歉,如果答案似乎很明显,但过去几个小时我一直在抨击我的头。

我一直在按照多个教程尝试docker调整我的应用程序。无论 url:port 还是 url 的组合,我都试过了,但我无法访问这些页面。

我对我做错了什么一无所知。我假设如下:

我尝试了多种设置,但都不起作用。

我有以下内容:

Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
ADD ./django.conf /etc/nginx/conf.d/default.conf

#COPY ./django.conf /etc/nginx/sites-available/
#RUN ln -s /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled

docker-compose.yml

version: '3'

services:
  db:
    image: postgres:9.6
    ports:
     - "5432:5432"
    environment:
      - POSTGRES_USER=random_user
      - POSTGRES_PASSWORD=random_password
  redis:
    restart: always
    image: redis:4.0
    expose:
      - "6379"

  nginx:
    image: nginx:1.14.0
    container_name: nginx01
    ports:
      - "8000:8000"
    volumes:
      - ./code
    depends_on:
     - web
  web:
    build: .
    container_name: backend-api
    command: bash -c "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn ops4_backend.wsgi -b 0.0.0.0:8000"
    depends_on:
      - db
    volumes:
      - ./code
    expose:
      - "8000"
    restart: always

django.conf

upstream web {
   ip_hash;
   server web:8000;
 }

 # portal
 server {
   location / {
        proxy_pass http://web:8000;
     }
   listen 8000;
   server_name localhost;

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

docker ps -a

昨天我在做几乎相同的任务,我看到的唯一有价值的区别是你在 Django 容器中包含 django.conf,而不是在 nginx 容器中。我在 docker-compose.ymlnginx 部分有以下卷:
- ./nginx:/etc/nginx/conf.d(其中 ./nginx 是带有 django.conf 的文件夹)

编辑:这是我的 docker-compose.yml:

version: '3'

services:
  nginx:
    image: nginx
    restart: always
    ports:
      - "80:8000"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./static_cdn:/static
    depends_on:
      - web
  db:
    image: postgres
    restart: always
  web:
    build: .
    restart: always
    command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --no-input && gunicorn core.wsgi -b 0.0.0.0:8000"
    volumes:
      - .:/code
    expose:
      - "8000"
    depends_on:
      - db

django.conf:

upstream web {
    ip_hash;
    server web:8000;
}

server {
    location /static/ {
        alias /static/;
    }

    location / {
        proxy_pass http://web/;
    }
    listen 8000;
    server_name localhost;
}

通过此设置,nginx 在 127.0.0.1:80127.0.0.1 可用,因为 80 是默认的 http 端口。

错误较多,请使用this配置。然后根据您的特定需求逐步更改它。

还要检查这个讨论 Nginx Docker Configurations 的页面,因为你的主要问题之一是你没有公开你制作的 django nginx conf:

ADD ./django.conf /etc/nginx/conf.d/default.conf

nginx容器。 所以 nginx 不知道如何映射您的配置。