Django channles線上部署(騰訊雲)
阿新 • • 發佈:2021-06-15
基本架構:
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; } } }