【问题标题】:Ngnix Django 502 Bad GatewayNginx Django 502 错误网关
【发布时间】:2020-09-29 23:17:59
【问题描述】:

我使用来自 gitlab 的 CI/CD 部署了一个 django 应用程序。该应用程序包装在 docker 容器中并使用 Nginx 服务器。从 git 部署成功,但访问 IP 时出现错误。

502 Bad Gateway
nginx/1.17.4

我检查了应用程序容器的日志并输出了

no destination
no destination
no destination
no destination
no destination
no destination
no destination

另外,Nginx 容器的输出


2020/06/06 16:49:29 [error] 6#6: *754 connect() failed (111: Connection refused) while connecting to upstream, client: 196.251.20.105, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.18.0.3:8000/favicon.ico", host: "18.189.11.120", referrer: "http://18.189.11.120/"
196.251.20.105 - - [06/Jun/2020:16:49:29 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://18.189.11.120/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

对于一些额外的上下文,我将发布我的一些文件的内容

Dockerfile

FROM python:3.6-slim

# create the appropriate directories

ENV APP_HOME=/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/static
RUN mkdir $APP_HOME/mediafiles
WORKDIR $APP_HOME



ENV PYTHONUNBUFFERED=1

# Add unstable repo to allow us to access latest GDAL builds
# Existing binutils causes a dependency conflict, correct version will be installed when GDAL gets intalled
RUN echo deb http://deb.debian.org/debian testing main contrib non-free >> /etc/apt/sources.list && \
    apt-get update && \
    apt-get remove -y binutils && \
    apt-get autoremove -y

# Install GDAL dependencies
RUN apt-get install -y libgdal-dev g++ --no-install-recommends && \
    pip install pipenv && \
    pip install whitenoise && \
    pip install gunicorn && \
    apt-get clean -y

# Update C env vars so compiler can find gdal
ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
ENV C_INCLUDE_PATH=/usr/include/gdal

ENV LC_ALL="C.UTF-8"
ENV LC_CTYPE="C.UTF-8"

# -- Adding Pipfiles
# COPY Pipfile Pipfile
# COPY Pipfile.lock Pipfile.lock
# COPY package.json package.json

# -- Install dependencies:
RUN pip install --upgrade pip
COPY ./requirements.txt /web/requirements.txt
RUN pip install -r requirements.txt

RUN apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && apt-get install -y netcat \
    && curl -sL https://deb.nodesource.com/setup_8.x | bash \
    && apt-get install nodejs -yq

# copy entrypoint.sh
COPY ./entrypoint.prod.sh /web/entrypoint.prod.sh

# copy project
COPY . /web/

# run entrypoint.sh
ENTRYPOINT ["/web/entrypoint.prod.sh"]

entrypoint.prod.sh

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

exec "$@"

docker-compose.yml

version: '3.7'

services:
  web:
    image: "${WEB_IMAGE}"
    command: gunicorn web.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/web/staticfiles
      - media_volume:/web/mediafiles
    ports:
      - 8000:8000
    env_file: .env
    depends_on:
      - db
  nginx:
    image: "${NGINX_IMAGE}"
    volumes:
      - static_volume:/web/staticfiles
      - media_volume:/web/mediafiles
    ports:
      - 80:80
    depends_on:
      - web
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file: .env

volumes:
  postgres_data:
  static_volume:
  media_volume:

Nginx Dockerfile

FROM nginx:1.17.4-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

nginx.conf

upstream web {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://web;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /web/static/;
    }

    location /mediafiles/ {
        alias /web/mediafiles/;
    }

}

【问题讨论】:

  • nginx 配置如何将请求转发到上游?
  • 我刚刚添加了它
  • proxy_pass http://up; -> proxy_pass http://paalup; 没有?
  • @JavierBuzzi 是的..对不起
  • 如果重新启动:docker-compose restart nginx 是否有效? (ps。你需要做一个docker-compose build nignx)——我会在depends_on下面/上面添加- nignx- db

标签: django docker nginx amazon-ec2 docker-compose


【解决方案1】:

这不应该是你的情况,但我遇到了类似的问题,错误是由于我的应用程序连接到外部数据库造成的。

尝试找出问题的一种方法是访问 Web 容器并运行 curl localhost:8000 以查看它是否在本地工作。

您可以进行的另一种尝试是使用网络,如下所示:

version: '3.7'

services:
  web:
    image: "${WEB_IMAGE}"
    command: gunicorn web.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/web/staticfiles
      - media_volume:/web/mediafiles
    ports:
      - 8000:8000
    env_file: .env
    depends_on:
      - db
    networks:
      - nginx-network
      - db-network
  nginx:
    image: "${NGINX_IMAGE}"
    volumes:
      - static_volume:/web/staticfiles
      - media_volume:/web/mediafiles
    ports:
      - 80:80
    depends_on:
      - web
    networks:
      - nginx-network
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file: .env
    networks:
      - db-network

volumes:
  postgres_data:
  static_volume:
  media_volume:

networks:
  nginx-network:
    driver: bridge
  db-network:
    driver: bridge

【讨论】:

    【解决方案2】:

    你应该在 docker-compose.yml 中建立从 web 到 nginx 的链接,

    nginx:
      ...
      links:
         - "web"
    

    你的 nginx 看不到上游 web。 更多信息 : https://docs.docker.com/compose/compose-file/#links

    【讨论】:

    • 这被标记为“遗留”——你为什么要提供这个作为答案?
    猜你喜欢
    • 2012-07-16
    • 2012-05-28
    • 2018-12-05
    • 2017-07-07
    • 2020-04-05
    • 2019-01-01
    • 2015-04-22
    • 2021-11-19
    • 2011-05-14
    相关资源
    最近更新 更多