繁体   English   中英

Nginx + Django:ModuleNotFoundError:没有名为“app”的模块

[英]Nginx + Django: ModuleNotFoundError: No module named 'app'

我正在尝试使用 Docker 中的 Nginx 和 Gunicorn 运行我的 Django 应用程序

在 docker 撰写日志中,我有以下错误:(完整日志: https://pastebin.com/EXd3Bsii

File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
django_1  |     mod = importlib.import_module(module)
django_1  |   File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
django_1  |     return _bootstrap._gcd_import(name[level:], package, level)
django_1  |   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
...
django_1  |   File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
django_1  | ModuleNotFoundError: No module named 'app'

我该如何解决这个问题?

我应该指定 wsgi 配置吗?

我的代码wsgi,py

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'potok.settings')
application = get_wsgi_application()

nginx-conf.conf

upstream app {
    server django:8000;
}

server {
    listen:80;
    server_name: localhost;

    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect_off;
    }

    location /staticfiles/ {
        alias /var/www/html/staticfiles/;
    }
}

docker-compose.yml

version: '3.9'

services:
  django:
    build: . # path to Dockerfile
    command: sh -c "gunicorn --bind 0.0.0.0:8000 app.wsgi"
    volumes:
      - .:/project
      - static_data:/project/static
    expose:
      - 8000
    environment: 
      - DATABASE_URL=postgres://postgres:XXXXXXXXX@localhost:5432/lk_potok_2"
      - DEBUG=1
  ...
  
  nginx:
    image: nginx:1.19.8-alpine
    depends_on: 
      - django
    ports: 
      - "80:80"
    volumes:
      - static_data:/var/www/html/static
      - ./nginx-conf.d:/etc/nginx/conf.d

volumes:
    pg_data:
    static_data:

你的nginx.conf有问题: proxy_pass http://django:8000;

upstream app {
    server django:8000;
}

server {
    listen:80;
    server_name: localhost;

    location / {
        proxy_pass http://django:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect_off;
    }

    location /staticfiles/ {
        alias /var/www/html/staticfiles/;
    }
}

Nginx and django are in the same network, nginx can access the django instances generated by gunicorn using the name of the container and the port guniconr binds the workers to.

您还需要修改 docker-compose.yaml。 Gunicorn 无法访问应用程序,因为您将其命名为application而不是应用程序。

version: '3.9'

services:
  django:
    build: . # path to Dockerfile
    command: sh -c "gunicorn --bind 0.0.0.0:8000 wsgi:application"
    volumes:
      - .:/project
      - static_data:/project/static
    expose:
      - 8000
    environment:  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM