gpt4 book ai didi

django - 在 docker 上使用 nginx gunicorn 配置 django

转载 作者:行者123 更新时间:2023-12-02 18:06:40 29 4
gpt4 key购买 nike

美好的一天,我是 docker 的新手,我有一个 Django 应用程序,我想 dockerize,我已经搜索了教程来帮助我使用 docker 设置我的 Django 应用程序,我在测试驱动的 https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/ 上关注了这篇文章。我在使 nginx 与我的应用程序一起工作时遇到问题。这是我的代码。

我的应用程序 docker 文件:

FROM python:3.8-alpine

ENV PATH="/scripts:${PATH}"

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt


RUN mkdir /app
COPY ./testdocker /app
WORKDIR /app
COPY ./scripts /scripts

RUN chmod +x /scripts/*

RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static

RUN adduser -D user
RUN chown -R user:user /vol

RUN chmod -R 755 /vol/web

USER user

nginx docker file:

FROM nginx:1.19.3-alpine

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

RUN mkdir -p /vol/static

my docker-compose file:

version: '3.7'

services:
app:
build:
context: .
command: sh -c "gunicorn testdocker.wsgi:application --bind 0.0.0.0:8000"
volumes:
- static_data:/vol/web
expose:
- "8000"
environment:
- SECRET_KEY=MANME1233
- ALLOWED_HOSTS=127.0.0.1, localhost

nginx:
build:
context: ./nginx
volumes:
- static_data:/vol/static
ports:
- "8080:80"
depends_on:
- app

volumes:
static_data:


my nginx conf file:

upstream testapp {
server app:8000;
}

server {
listen 8080;
server_name app;

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

location /static {
alias /vol/static;
}

}

我似乎无法让 nginx 反向代理到我的网络应用程序,在浏览器上打开 URL 时,我收到 404 错误请求或找不到地址。请问我做错了什么或做错了什么?

最佳答案

@victormazeli 看起来你错过了将你的服务放在同一个 docker 网络中,我在 nginx conf 文件中看到一些错误配置。尝试按如下方式更新您的 docker-compose.yml:

version: '3.7'

services:
app:
build:
context: .
command: sh -c "gunicorn testdocker.wsgi:application --bind 0.0.0.0:8000"
volumes:
- static_data:/vol/web
expose:
- "8000"
environment:
- SECRET_KEY=MANME1233
- ALLOWED_HOSTS=127.0.0.1, localhost
networks:
- main

nginx:
build:
context: ./nginx
volumes:
- static_data:/vol/static
ports:
- "8080:80"
depends_on:
- app
networks:
- main

volumes:
static_data:
networks:
main:

然后,更新您的 nginx 配置如下:

server {
server_name nginx;
listen 80;

location /static {
alias /vol/static;
}
location / {
proxy_pass http://app:8000/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}

}

这里要记住的另一件事是,您有 2 个目标由 NGINX 反向代理服务:

  1. 位于 testdocker 的 Django 项目应该可以通过 localhost:8080 访问
  2. 可通过 localhost:8080/static/[relative_path] 访问的静态文件数据

要访问静态数据,您需要相对于 nginx 服务中的 /vol/static 的路径(这是一个 docker volume mount 也挂载到 /vol/webapp 服务中)。根据 appDockerfilestatic_data 卷应该包含 2 个目录:mediastatic。因此,如果您在 app 服务中说 index.html 位于目录 /vol/web/static 中,它应该可以通过 localhost:8080/static/static/index.html.

试一试,让我知道结果如何。

关于django - 在 docker 上使用 nginx gunicorn 配置 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64438853/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com