首页 > 解决方案 > 在 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 错误请求或找不到地址。请问我做错了什么或做错了什么?

标签: djangodockernginx

解决方案


@victormazeli 看起来您错过了将服务放在同一个 docker 网络中,我在nginxconf 文件中看到了一些错误配置。尝试更新您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]

要访问静态数据,您将需要相对于服务中的路径/vol/static(这是一个也安装到服务nginx中的 docker 卷挂载)。根据's ,该卷应包含 2 个目录:和. 因此,如果您说位于服务中的目录,它应该可以通过./vol/webappappDockerfilestatic_datamediastaticindex.html/vol/web/staticapplocalhost:8080/static/static/index.html

试试这个,让我知道它对你有用。


推荐阅读