Ngnix Django 502坏网关
我使用gitlab中的CI/CD部署了一个django应用程序。该应用程序包装在docker容器中,并使用Nginx服务器。从git部署成功,但在访问IP时出现错误Ngnix Django 502坏网关,django,docker,nginx,amazon-ec2,docker-compose,Django,Docker,Nginx,Amazon Ec2,Docker Compose,我使用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容器的
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"]
FROM nginx:1.17.4-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
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 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"]
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/;
}
}
您应该在docker-compose.yml中创建从web到nginx的链接
nginx:
...
links:
- "web"
你的nginx看不到上游网站。
更多信息:
这不应该是您的情况,但我遇到了类似的问题,错误是由于我的应用程序连接到外部数据库造成的 试图找到问题的一种方法是访问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
nginx配置如何将请求转发到上游?我刚刚添加了
proxy\u passhttp://up;代码>->proxy\u passhttp://paalup;代码>否?@Javierbuszi yes..Sorry如果重新启动:docker compose restart nginx
?(顺便说一句,你需要做一个docker compose build nignx
)--我会在下添加-nignx
依赖于下/上面-db
这被标记为“遗留”--你为什么要给出这个答案?