2.1 基本使用
-
官方推荐使用asgi服务器Daphne处理websocket请求
-
运行Daphne
$ cd 项目目录 $ daphne -b 0.0.0.0 -p 8060 --proxy-headers 项目名.asgi:application
2.2 使用进程管理supervisor运行Daphne
-
配置
supervisord.conf
# 新增Daphne任务 [program:daphne_asgi] directory=/root/helloxjn command=/opt/python3.7.9/bin/daphne -b 0.0.0.0 -p 8060 --proxy-headers helloxjn.asgi:application autostart=true autorestart=true stdout_logfile=/tmp/websocket.log redirect_stderr=true
2.3 Nginx配置
-
pe
server { listen 443 ssl; ... # 新增wss配置 location /home/like_num/ { proxy_pass http://0.0.0.0:8060; # Daphne任务端口 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; proxy_read_timeout 3600s; # 长连接时间 } }