uWSGI和Django - 运行wsgi时出错

时间:2014-10-12 16:44:42

标签: python django nginx uwsgi

工作正常

我跟随wsgi documentation to run django。在开始使用nginx之前,我测试它是否全部正常工作。我成功运行manage.py并在浏览器中加载网页:

python manage.py runserver 0.0.0.0:8000

enter image description here

什么不工作

当我尝试使用uwsgi运行它时出现问题:

uwsgi --http :8000 --module metrics.wsgi

我可以run it without errors,但是当我尝试在浏览器中加载它时,我从uwsgi获得AppRegistryNotReady error。关于可能是什么原因的任何想法?这是我的wsgi.py文件:

import os, sys, site, django.core.handlers.wsgi

SITE_DIR = '/home/ubuntu/web/metrics.com/app/'
site.addsitedir(SITE_DIR)
sys.path.append(SITE_DIR)

os.environ['DJANGO_SETTINGS_MODULE'] = 'metrics.settings'
application = django.core.handlers.wsgi.WSGIHandler()

我的项目结构:

/ubuntu
    /www
        /metrics.com 
            /app         # here's my manage.py file
                metrics/ # here's my wsgi.py and settigs.py files

2 个答案:

答案 0 :(得分:1)

<强>解: wsgi.py中的错误配置使uWSGI无法调用该应用程序。我用这个wsgi.py解决了它:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "metrics.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

像这样运行uwsgi:

uwsgi --http :8000 --chdir /home/ubuntu/web/metrics.com/app --module gamemetrics.wsgi

编辑:使用--chdir我们设置用于--module的基目录。

编辑2 :在某些情况下,这可以修复NGINX错误:从上游读取响应头时上游过早关闭连接

答案 1 :(得分:0)

1): 强烈建议在皇帝模式下运行uwsgi。

/usr/bin/uwsgi --emperor /etc/uwsgi --pidfile /var/run/uwsgi.pid --daemonize /var/log/uwsgi.log

2)项目的示例wsgi.py:

import os
import sys
ADD_PATH = ['/home/ubuntu/web/metrics.com/app/',]
for item in ADD_PATH:
    sys.path.insert (0, item)
os.environ['PYTHON_EGG_CACHE']= '/tmp/your-project-eggs'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

3)项目的uwsgi配置示例(将其放在/ etc / uwsgi中)&lt; - 见i.1
------- project.yaml -------

uwsgi:
    print: Project Configuration Started
    socket: /var/tmp/metrics_uwsgi.sock
    pythonpath: /home/ubuntu/web/metrics.com
    env: DJANGO_SETTINGS_MODULE=app.settings
    module: app.wsgi
    chdir: /home/ubuntu/web/metrics.com/app
    daemonize: /home/ubuntu/web/metrics.com/log/uwsgi.log
    pidfile: /var/run/metrics_uwsgi.pid
    max-requests: 5000
    buffer-size: 32768
    harakiri: 30
    reload-mercy: 8
    master: 1
    no-orphans: 1
    touch-reload: /home/ubuntu/web/metrics.com/log/uwsgi
    post-buffering: 8192

4)包含在你的nginx配置

location /
    {
        uwsgi_pass unix:///var/tmp/metrics_uwsgi.sock;
        include uwsgi_params;
        uwsgi_buffers 8 128k;
     }