django - nginx配置,参考一个dockerized django app
问题描述
我正在尝试做什么以及我做了什么
我正在尝试通过 amazon lightsail 容器中的 docker 容器部署 django 应用程序。
我按照本教程使用 docker 容器。我跳过了 Postgres 部分并使用了其余部分。有效。
我有一个多容器设置。nginx 充当反向代理,获取所有请求并提供静态文件和媒体。另一个容器是 django 应用程序。我使用 docker-compose 来构建和启动图像。据我了解,我不能将 docker-compose 与 lightsail 容器一起使用。我必须分别定义这两个容器。这是问题所在。docker-compose 在其配置中定义我的 Web 服务(django 应用程序)。nginx 配置将 Web 服务器作为带有upstream指令的代理服务器,并使用 docker-compose 中定义的名称引用它。
我试图将 web 定义为 127.0.0.1 但这不起作用。
我的生产配置
这些是我管理容器的文件。
Dockerfiles
这是我的应用程序中的 Dockerfile:
# pull official base image
FROM python:3.9.6
# set work directory its an absolute path
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
这是 nginx 的 Dockerfile,它基本上删除了默认配置并将我的放在容器中:
FROM nginx:1.21
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
nginx 配置
这是 nginx.conf:
upstream sito {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://sito;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
client_max_body_size 100M;
}
location /static/ {
autoindex on;
alias /home/app/web/static/;
}
location /media/ {
autoindex on;
alias /home/app/web/media/;
}
}
如您所见,配置文件使用upstream然后定义一个名为web的服务器。我不能使用该名称,因为它是在我放在下面的 docker-compose 文件中定义的
码头工人撰写
docker-compose 用于在开发中编排两者:
version: '3.8'
services:
web:
build:
context: ./sito
dockerfile: Dockerfile
command: gunicorn sito.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
expose:
- 8000
env_file:
- ./.env.dev
nginx:
build: ./nginx
ports:
- 1337:80
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
depends_on:
- web
volumes:
static_volume:
media_volume:
我的问题
现在部署到 lightsail 容器。我不能使用 docker-compose 来启动所有内容,因此我必须在 aws 上创建容器时重新创建那里指定的设置。
我想使用需要“分发”的容器,即图像+容器。我的图片已上传到 dockerHub。
我创建了 2 个容器,
第一个是 nginx(名称为“nginx”),图像是在我上传我的版本后从 dockerHub 获取的(所以它不是原始的 nginx)。在我的 docker-compose 中,我指定了端口,内部端口是 80,外部端口是 1337。虽然菜单我可以指定访问端口,但我不确定是否必须指定(或如何)另一个端口( 80)。
第二个容器是应用程序(名称为“sito”),图像也来自我上传的 dockerhub。我的docker -compose 指定了一个启动 gunicorn 的命令,使其监听端口 8000,我可以在创建容器时放置该命令,并公开端口 8000。
分发不起作用。这些是两个容器的日志:
Nginx 日志
[10/ott/2021:20:33:03] [deployment:4] Creating your deployment
[10/ott/2021:20:34:00] /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
[10/ott/2021:20:34:00] 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
[10/ott/2021:20:34:00] /docker-entrypoint.sh: Configuration complete; ready for start up
[10/ott/2021:20:34:00] 2021/10/10 20:34:00 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:34:00] nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:34:54] [deployment:4] Started 1 new node
[10/ott/2021:20:35:56] /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
[10/ott/2021:20:35:56] 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
[10/ott/2021:20:35:56] /docker-entrypoint.sh: Configuration complete; ready for start up
[10/ott/2021:20:35:56] 2021/10/10 20:35:56 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:35:56] nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2
[10/ott/2021:20:37:22] [deployment:4] Started 1 new node
[10/ott/2021:20:37:40] [deployment:4] Took too long
我认为问题在于他找不到该应用程序,因此它给了我错误主机未在上游找到
现场日志
[10/ott/2021:20:33:03] [deployment:4] Creating your deployment
[10/ott/2021:20:34:02] [2021-10-10 20:34:02 +0000] [1] [INFO] Starting gunicorn 20.1.0
[10/ott/2021:20:34:02] [2021-10-10 20:34:02 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[10/ott/2021:20:34:02] [2021-10-10 20:34:02 +0000] [1] [INFO] Using worker: sync
[10/ott/2021:20:34:02] [2021-10-10 20:34:02 +0000] [7] [INFO] Booting worker with pid: 7
[10/ott/2021:20:34:54] [deployment:4] Started 1 new node
[10/ott/2021:20:35:58] [2021-10-10 20:35:58 +0000] [1] [INFO] Starting gunicorn 20.1.0
[10/ott/2021:20:35:58] [2021-10-10 20:35:58 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[10/ott/2021:20:35:58] [2021-10-10 20:35:58 +0000] [1] [INFO] Using worker: sync
[10/ott/2021:20:35:58] [2021-10-10 20:35:58 +0000] [7] [INFO] Booting worker with pid: 7
[10/ott/2021:20:37:22] [deployment:4] Started 1 new node
[10/ott/2021:20:37:40] [deployment:4] Took too long
这更糟糕,因为它只是简单地说花了太长时间,我想连 django 都没有开始。
我对部署非常陌生,尤其是使用容器,所以我可能犯了很多错误,遗憾的是我找不到任何关于这项工作的正确解释,因为亚马逊文档很糟糕(在我看来)。
注意 我知道 docker-compose 文件中指定的卷不是由两个容器创建的,但我认为这不是问题,因为它可能会给静态文件提供问题,但它应该能够正常工作
感谢您的关注和帮助。
解决方案
推荐阅读
- javascript - 我如何在角度/打字稿中传递回调
- c# - 如何保持 asp.net 传递到存储过程的字符串的一致性?
- java - JSON 响应中的重复字段
- c# - 提供一个期望 Action 作为回调的对象
- scala - Scala:如何将 ValidatedNel 的 HList 转换为 HList 的 ValidatedNel
- javascript - 将数组与字符串进行比较,并将在数组中找到的所有项目替换为字符串中的公共字符
- c# - 验证数据网格 C# 中的复选框
- reporting-services - 为表格报表中的单元格创建集合表达式
- kernel - 高斯过程的常数核
- javascript - 测试 sagas:调用效果的存根响应