一、前言
pip3 install channel
二、应用
直接看官方教程,这里就不啰嗦了
三、部署
这里给出我的方案
安装supervisord
sudo apt install supervisor
在项目settings.py同级目录下建asgi.py
"""
ASGI entrypoint. Configures Django and then runs the application
defined in the ASGI_APPLICATION setting.
"""
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyBlog.settings")
django.setup()
application = get_default_application()
修改配置文件,路径如下
/etc/supervisor/supervisord.conf
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[program:daphne]
directory=/home/MyBlog
command=daphne -b 127.0.0.1 -p 8001 --proxy-headers MyBlog.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log #日志
redirect_stderr=true
[include]
files = /etc/supervisor/conf.d/*.conf
这里主要加入了
[program:daphne]
directory=/home/MyBlog # 你的项目路径,不是app路径
command=daphne -b 127.0.0.1 -p 8001 --proxy-headers MyBlog.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log #日志
redirect_stderr=true
注意,记得检查是否存在日志文件,没有的话touch一个
查看进程命令
ps -ef | grep supervisord
启动命令
supervisord -c /etc/supervisor/supervisord.conf
如果报错:
Unlinking stale socket /var/run/supervisor.sock
用如下命令:
find / -name supervisor.sock
/run/supervisor.sock
然后
unlink /run/supervisor.sock
再次启动
supervisord -c /etc/supervisor/supervisord.conf
然后修改nginx.conf文件
upstream channels-backend {
server localhost:8001;
}
# 在http中加入这个
# 在server中加入这个
# websockets
location /ws/chat/ {
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
http {
upstream channels-backend {
server localhost:8001;
}
server {
listen 80;
server_name _;
return 301 https://www.guanacossj.com$request_uri;
}
server {
listen 443 default_server;
listen [::]:443 default_server;
server_name _;
ssl on;
ssl_certificate /etc/nginx/cert/1831344_www.guanacossj.com.pem; # 路径/pem文件
ssl_certificate_key /etc/nginx/cert/1831344_www.guanacossj.com.key; # 路径/key文件
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
charset utf-8;
client_max_body_size 1000M; # adjust to taste
include /etc/nginx/default.d/*.conf;
location /static {
alias /home/MyBlog/static; # ָÏdjangoµÄtaticĿ¼
}
location /static/rest_framework/ {
alias /usr/local/lib/python3.6/dist-packages/rest_framework/static/rest_framework/
;}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 127.0.0.1:8000;
#uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
uwsgi_send_timeout 600;
uwsgi_connect_timeout 600;
uwsgi_read_timeout 600;
}
# websockets
location /ws/chat/ {
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
...
}
注意查看你的html文件中的websocket请求地址
官方教程给的是
var chatSocket = new WebSocket('wss://' + 'www.guanacossj.com' +'/ws/chat/' + roomName + '/');
如果你用的https,像我一样,那就是wss开头
然后后面一定要是域名不能用localhost
注意这里的
/ws/chat/
和nginx中的location那里的一定要一致。