Django,Gunicorn设置

时间:2015-10-17 16:43:24

标签: django python-2.7 nginx gunicorn

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

partial

gunicorn_config.py:

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

nginx project.conf:

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

我运行以下操作尝试启动它:

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;
    }
}

但它只是说"无法连接(' 127.0.0.1',80)"

2 个答案:

答案 0 :(得分:2)

基本上,我猜想nginx会在gunicorn之前旋转。它需要80端口(来自监听)。 gunicorn接下来,也想要端口80(来自你的绑定),并发现它被占用,所以它出错了。在另一个端口上运行gunicorn并使用proxy_pass告诉nginx它。

Gunicorn

bind = '127.0.0.1:8000'

Nginx的

proxy_pass http://127.0.0.1:8000/;

答案 1 :(得分:1)

您已配置gunicorn绑定在TCP端口上,但是gunicorn绑定在unix套接字上。你应该使用同样的东西;最好是套接字,所以它与nginx实际监听的端口没有冲突。

在gunicorn_config.py中:

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