nginx >> supervisor >> Daphne >> websocket
安装daphne
#pip install channels #安装结束后会在当前虚拟环境下产生daphne
配置django asgi
配置asgi.py文件(假设你已经做好了websocket的路由转向),如果不增加以下这两行,就会报错 ########### import django ... django.setup() ...
测试daphne
$(pwd)\venv\daphne -p 8001 -b 0.0.0.0 <your_project_name>.asgi:application #以上测试通过后(向该地址+路由发送ws数据),如果退出终端,该进程任务将会结束
安装supervisor持续进程任务
#centos yum install supervisor #ubunto : apt-get install supervisor #pip pip install supervisor
编写配置文件
#/etc/supervisord.conf, 这是主配置文件,我们不改它 #/etc/supervisord.d/self_config.ini 新增这个文件,编写配置内容,文件名称随意 [program:daphne] directory=/www/personMapProject #项目目录 command=/www/personMapProject/venv/bin/daphne -b 127.0.0.1 -p 8889 personMapProject.asgi:application #绑定端口 autostart=true autorestart=true stdout_logfile=/www/personMapProject/websocket.log #日志路径 redirect_stderr=true #以上内容不要有注释
使用supervisor
supervisorctl status //查看所有进程的状态 supervisorctl stop es //停止es supervisorctl start es //启动es supervisorctl restart //重启es supervisorctl update //配置文件修改后使用该命令加载新的配置 supervisorctl reload //重新启动配置中的所有程序,自动加载子配置文件
配置nginx
user root; worker_processes 2; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; gzip on; upstream channels-backend { server localhost:8889; } server { listen 80; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8888; uwsgi_send_timeout 600; } location /static { alias /www/personMapProject/collect_static/; } location /media { alias /www/personMapProject/media/; } location = /50x.html { root html; } location /ws { proxy_pass http://channels-backend; 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; } } }