django频道的gunicorn设置

时间:2017-07-16 16:07:35

标签: django nginx gunicorn django-channels

我的django应用程序在本地计算机和Heroku上运行良好,但是我遇到麻烦部署它会做Digital Ocean Ubuntu 16.04 droplet。随后的this教程,应用程序已启动,但websockets无法正常工作。 通过在终端虚拟环境中运行:daphne -b 0.0.0.0 -p 8000 slist.asgi:channel_layer 在第二个终端窗口中:

./manage.py runworker

该应用程序可以正常工作,但仅限于端口8000.我无法在mydomain.com上访问它,但只能在mydomain.com:8000上访问它。 当我关闭终端窗口时,无法访问该站点。 在没有我的终端窗口且没有':8000'

的情况下,需要为站点设置什么以及如何设置?

我的gunicorn.service文件:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=kuba1
Group=www-data
WorkingDirectory=/home/kuba1/slistproject
ExecStart=/home/kuba1/slistproject/slistvenv/bin/gunicorn --workers 3 --bind unix:/home/kuba1/slistproject/slistvenv/src/slist.sock slist.wsgi:application

[Install]
WantedBy=multi-user.target

我的Nginx设置:

server {
    listen 80;
    server_name server_name fortests.ovh;
    client_max_body_size 20M;

    location /static/ {
        root /home/kuba1/slistproject/slistvenv/src;
    }

    location / {


        proxy_pass http://unix:/home/kuba1/slistproject/slistvenv/src/slist.sock;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;

        }

}

2 个答案:

答案 0 :(得分:0)

您必须将域代理传递到该端口8000:

location / {
        proxy_pass_header Server;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8001/;
}

答案 1 :(得分:0)

应该是:

proxy_set_header Connection "Upgrade";

为我配置工作:

upstream websocket {
    server  unix:/path/to/daphne.sock fail_timeout=20;
}

location /url/socket/ {
    try_files $uri @proxy_to_channel_app;
}

location @proxy_to_channel_app {

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    # enable this if and only if you use HTTPS
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;

    # web socket setup
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'Upgrade';

    # we don't want nginx trying to do something clever with
    # redirects, we set the Host: header above already.
    proxy_redirect off;
    proxy_pass http://websocket;
}
达芙妮命令:

daphne -u run/daphne.sock project.asgi:channel_layer