【问题标题】:Django, Gunicorn SetupDjango,Gunicorn 设置
【发布时间】:2015-10-17 16:43:24
【问题描述】:

我正在尝试在我的服务器上设置一个 django 项目,但无法让它运行。我将 virtualenv、gunicorn 和 nginx 用于静态文件。我不确定我是不是错了。我目前的设置如下:

myenv
  - project(my django project)
  - bin(and all it contains)
  - lib(and all it contains)
  - include(and all it contains)
  - gunicorn_config.py

gunicorn_config.py:

command = '/home/me/django/myenv/bin/gunicorn'
pythonpath = '/home/me/django/myenv/project'
bind = '127.0.0.1:80'
workers = 2

nginx 项目.conf:

upstream project_server {
    server unix:/tmp/gunicorn_project.sock fail_timeout=0;
}

server {
    listen 80;
    client_max_body_size 4G;

    # set the correct host(s) for your site
    server_name project.com www.project.com;

    keepalive_timeout 5;

    # path for static files
    root /home/me/django/myenv/assets;

    location / {
        # checks for static file, if not found proxy to app
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://project_server;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/me/django/myenv/project/project/templates;
    }
}

然后我运行以下命令来尝试启动它:

/home/me/django/myenv/bin/gunicorn -c /home/me/django/myenv/gunicorn_config.py project/project/wsgi.py

但它只是说“无法连接到 ('127.0.0.1', 80)”

【问题讨论】:

  • 80 上没有 gunicorn 和 nginx 吗?你需要为 gunicorn 使用另一个端口。
  • 这看起来与您需要的相当接近注意 gunicorn 在 8000 上。开始忽略很多东西,比如静态。此时您真的很关心 proxy_pass 指令。 goodcode.io/articles/django-nginx-gunicorn

标签: django python-2.7 nginx gunicorn


【解决方案1】:

您已将 gunicorn 配置为绑定在 TCP 端口上,但 gunicorn 绑定在 unix 套接字上。你应该使用同样的东西;最好是socket,这样就不会和nginx实际监听的端口冲突了。

在 gunicorn_config.py 中:

bind = 'unix:/tmp/gunicorn_project.sock'

【讨论】:

  • 我更改了绑定选项,但是现在当我运行“/home/me/django/myenv/bin/gunicorn -c /home/me/django/myenv/gunicorn_config.py project/project/ wsgi.py”它说“工作进程中的异常:”.....“ImportError:找不到应用程序,您的意思是'项目/项目/wsgi:应用程序'吗?”
  • 原因:Worker 启动失败。
  • 您是否尝试按照错误消息的建议进行更改?
  • 听起来像是 python 路径或 gunicorn 参数问题。重新 python 路径,你在这个 bash 会话中激活了你的 virtualenv 吗?
  • 我自己的 gunicorn 命令行看起来像 gunicorn websec.wsgi我已经 cd'ed 进入要找到 manage.py 的目录,而 websec 是我在 wsgi.py 所在目录下的“主​​应用程序”。请注意,指定 gunicorn 的完整路径并没有什么坏处,但我不会对设置当前目录可以解决您的问题感到惊讶。
【解决方案2】:

基本上,我猜 nginx 在 gunicorn 之前启动。它占用端口 80(来自您的 listen)。 gunicorn 紧随其后,也需要 80 端口(来自您的 bind),发现它被占用,因此出错。在不同的端口上运行 gunicorn 并使用 proxy_pass 告诉 nginx。

独角兽

bind = '127.0.0.1:8000'

Nginx

proxy_pass http://127.0.0.1:8000/;

【讨论】:

  • 我更新了 nginx 配置文件,所以该行是“listen 8000;”。我将 gunicorn 绑定设置更改为以下。
猜你喜欢
  • 2013-07-07
  • 2015-01-08
  • 1970-01-01
  • 2013-03-28
  • 2019-01-18
  • 2016-01-04
  • 2018-03-27
  • 1970-01-01
  • 2016-04-16
相关资源
最近更新 更多