【问题标题】:Using wsgi.py correctly in Django在 Django 中正确使用 wsgi.py
【发布时间】:2015-03-25 17:24:43
【问题描述】:

当我部署我的 Django 站点时,我对 wsgi.py 文件以及它在本地使用与在生产中使用 Gunicorn 的方式有点困惑。

本地我有这个文件结构。

demo/
    __init__.py
    settings.py
    urls.py
    wsgi.py
manage.py

Settings.py 有如下设置...WSGI_APPLICATION = 'demo.wsgi.application'

但是,一旦我部署了在 Gunicorn 上运行的应用程序,我收到一条错误消息,说它找不到 wsgi.py 文件,为了让它工作,我必须像这样再次创建文件...

demo/
    __init__.py
    settings.py
    urls.py
    wsgi.py
manage.py
wsgi.py

这现在有效,但它向我暗示 Gunicorn 忽略了 Django 设置文件 demo/settings.py 中的 WSGI_APPLICATION 设置,对吧?如果是这样,Gunicorn 在哪里获得对 wsgi.py 文件位置的自己的引用以及本地有什么不同?

这是我的 Gunicorn 设置以防万一……

[program:gunicorn_process]
command=gunicorn wsgi:application -c /srv/test/gunicorn.conf.py
directory=/srv/test
user=root
autostart=true
autorestart=true
redirect_stderr=true

gunicorn.conf.py

bind = "127.0.0.1:8001"
workers = 3
worker_class = 'gevent'

【问题讨论】:

    标签: python django wsgi gunicorn


    【解决方案1】:

    Gunicorn 忽略了WSGI_APPLICATION 设置是正确的。此设置仅用于指定runserver 命令使用的wsgi 应用程序。

    Gunicorn 对 Django 一无所知,但您可以指定 Gunicorn 应在哪个模块中查找应用程序。现在它在wsgi 模块中寻找@​​987654323@ 属性:

    command=gunicorn wsgi:application ...
    

    要使用demo/中的文件,必须在Gunicorn命令中指定完整的模块路径:

    command=gunicorn demo.wsgi:application ...
    

    【讨论】:

    • 感谢您的回答,现在说得通了。我假设同一文件中的directory 设置是gunicorn_process 如何知道如何在 /srv/test 的根目录中查找 wsgi.py
    猜你喜欢
    • 2013-12-24
    • 2013-09-27
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多