使用SSL部署Daphne + NGINX的Django频道

时间:2017-04-24 02:54:05

标签: python django ssl nginx daphne

我有一个nginx的工作配置代理到django频道的上游daphne服务器。但是,当我将我的站点移动到ssl时,我开始遇到与websocket请求有关的403错误。这来自我的错误日志:

None - - [24/Apr/2017:02:43:36] "WSCONNECTING /pulse_events" - -
None - - [24/Apr/2017:02:43:36] "WSREJECT /pulse_events" - -
2017/04/24 02:43:37 [info] 465#465: *10 client 69.203.115.135 closed keepalive 
connection

来自访问日志:

- - [24/Apr/2017:02:48:54 +0000] "GET /pulse_events HTTP/1.1" 403 5 "-" "-"
- - [24/Apr/2017:02:49:03 +0000] "GET /pulse_state/ HTTP/2.0" 200 1376 "-" "Pulse/1 CFNetwork/811.4.18 Darwin/16.1.0"

我的nginx配置如下:

upstream pulse_web_server {
    server unix:/home/pulseweb/run/gunicorn.sock fail_timeout=0;
}

upstream pulse_web_sockets {
   server unix:/home/pulseweb/run/daphne.sock;
}

map $http_upgrade $connection_upgrade {
     default upgrade;
     '' close;
}

server {
    listen 80;
    server_name backend.com;
    return 301 https://$host$request_uri;
}


server {
    listen         443 http2 ssl;
    server_name    backend.com;

    root /var/www/vhosts/backend.com;
  location ~ /.well-known {
       allow all;
  }

  include snippets/ssl-params.conf;
  ssl_certificate /etc/letsencrypt/live/backend.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/backend.com/privkey.pem;



    client_max_body_size 4G;

    access_log  /var/log/nginx/pulse-access.log;
    error_log   /var/log/nginx/pulse-error.log info;

    location /static/ {
        alias /var/www/vhosts/backend.com/static/;
    }

    location /pulse_events {
    proxy_pass http://pulse_web_sockets;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location / {
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_set_header Connection "";
      proxy_http_version 1.1;
      server_tokens off;
      proxy_buffering on;
      if (!-f $request_filename) {
          proxy_pass http://pulse_web_server;
          break;
      }
  }
}

这是我的要求.txt:

asgi-redis==0.14.0
asgiref==0.14.0
asyncio==3.4.3
autobahn==0.16.0
channels==0.17.2
daphne==0.14.3
Django==1.10
django-extensions==1.7.2
django-webpack-loader==0.3.3
djangorestframework==3.4.4
msgpack-python==0.4.8
python-dateutil==2.5.3
redis==2.10.5
requests==2.11.0
six==1.10.0
Twisted==16.2.0
txaio==2.5.1
zope.interface==4.2.0

非常感谢任何见解。

3 个答案:

答案 0 :(得分:1)

我确实有Django + Daphne + nginx + ssl的有效配置,没有任何问题,我通过具有以下配置文件的超级用户运行daphne:

[program:project]

directory=<project_directory>
command=daphne -u <path_to_socket>/daphne.sock --root-path=<project_directory> <project>.asgi:channel_layer
stdout_logfile = <log_path>
stderr_logfile= <error_log_path>

[program:project_asgi_workers]

command=python <project_directory>/manage.py runworker
stdout_logfile=<log_file_path_2>
stderr_logfile=<log_error_path_2>
process_name=asgi_worker%(process_num)s
numprocs=2
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8                        ; Set UTF-8 as default encoding
autostart=true
autorestart=true
redirect_stderr=true
stopasgroup=true

要停止和启动这些工作程序,请运行以下命令:

  • sudo supervisorctl stop all
  • sudo supervisorctl start all

在nginx内部,我具有以下配置以连接到我的websocket:

location /pulse_events/ {
    proxy_pass http://unix:<path_to_socket>/daphne.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;
}

我在此项目上使用了达芙妮1.4.1版,asgi-redis 1.4.3,redis 2.10.6和通道1.1.8。

如果您仍然遇到问题,也许最好检查一下Django频道的路由和使用者。

答案 1 :(得分:0)

您的nginx期望一个 wss 请求,而不是一个 ws 请求。

答案 2 :(得分:0)

我使用Django + Daphne + Nginx + SSL,这是我的nginx mysite.conf。您的conf文件因处理/ ws请求而丢失。 ws和wss将使用此参数进行处理。请确保您拥有像Daphne这样的后端套接字服务器(我看到了),并在bash中运行该服务器并在8443中接受请求(这很重要,因为大多数服务器只允许在此套接字中使用)。并享受它。.

location /ws {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_pass http://127.0.0.1:8443;
}