项目中使用了webscoket进行实时通讯,但是生产环境又使用了django+nginx+uwsgi的部署方式,我们都知道uwsgi并不能处理websocket请求,所以需要asgi服务器来处理websocket请求,官方推荐的asgi服务器是daphne
安装supervisor
pip install supervisor
生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
使用supervisor管理daphne进程
编辑/etc/supervisord.conf加入配置
[program:daphne]
directory=/home/mysite #项目目录
command=daphne -b 0.0.0.0 -p 8080 mysite.asgi:application #启动命令
autostart=true
autorestart=true
stdout_logfile=/tmp/websocket.log #日志
redirect_stderr=true
启动supervisor
supervisord -c /etc/supervisord.conf
启动或者停止daphne
# 注意,使用docker的,需要使用管理员进入才能执行成功
# docker exec -it -u root container_name bash
supervisorctl start daphne
supervisorctl stop daphne