django - 在 docker 上使用 nginx gunicorn 配置 django
问题描述
美好的一天,我是 docker 新手,我有一个 Django 应用程序我想 dockerize,我已经搜索了帮助我使用 docker 设置我的 Django 应用程序的教程,我在测试驱动https://testdriven上关注了这篇文章.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/。我在使 nginx 与我的应用程序一起工作时遇到问题。这是我的代码。
我的应用程序泊坞窗文件:
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 反向代理提供服务:
- 位于
testdocker
其中的 Django 项目应该可以通过localhost:8080
- 可通过以下方式访问的静态文件数据
localhost:8080/static/[relative_path]
要访问静态数据,您将需要相对于服务中的路径/vol/static
(这是一个也安装到服务nginx
中的 docker 卷挂载)。根据's ,该卷应包含 2 个目录:和. 因此,如果您说位于服务中的目录,它应该可以通过./vol/web
app
app
Dockerfile
static_data
media
static
index.html
/vol/web/static
app
localhost:8080/static/static/index.html
试试这个,让我知道它对你有用。
推荐阅读
- python - 将 setup.py 与 Python Click 一起使用 - 我应该向 git 提交什么?
- html - 这个标签是什么(
<meta name=“viewport” content=“width=device-width, initial-scale=1”>
我对这个标签的作用的理解有点模糊。我会描述它,也许你可以修改/添加它。
- javascript - 如何使用 Flask-SocketIO、Python、Javascript 创建简单的“聊天应用程序”?
- spring-boot - Keycloak 授权服务 - 动态资源和资源属性
- android - 如何使用风味测试进行发布构建?
- java - 如何使用硒从网格中查找行列表
- excel - 在添加到数据模型的数据透视表中使用 VBA 取消选择一项
- python - 如何从一个脚本运行多个 Python/Shell 脚本
- kubernetes - 如果另一个作业正在运行,则 K8S Forbit 作业
- css - 显示属性在 chrome 中不起作用