一、概述
在上一篇文章中,鏈接如下:https://www.cnblogs.com/xiao987334176/p/14361893.html
開發了一個django channles websocket 項目,用的是asgi。官方推薦使用asgi服務器daphne,來處理websocket請求
daphne
Daphne 是一個純Python編寫的應用於UNIX環境的由Django項目維護的ASGI服務器。它扮演着ASGI參考服務器的角色。
安裝 Daphne
你可以通過 pip 來安裝 Daphne
python -m pip install daphne
在 Daphne 中運行 Django
一旦 Daphne 安裝完畢,你就可以使用 daphne
命令了,它將用來啟動 Daphne 服務進程。在最簡單的情形下,Daphne 加上包含一個 ASGI 應用模塊的位置和應用的名稱(以冒號分隔)。
對於一個典型的 Django 項目,可以像下面這樣來啟動 Daphne
daphne myproject.asgi:application
它將開啟一個進程,監聽 127.0.0.1:8000。這需要你的項目位於 Python path 上。為了確保這點,你應該在與 manage.py 文件相同的路徑中運行這個命令。
如果需要更改運行端口,使用以下命令:
daphne myproject.asgi:application -b 0.0.0.0 -p 8000
說明:
-b 監聽地址
-p 監控端口
二、實際項目運行
在上一篇文章中,鏈接如下:https://www.cnblogs.com/xiao987334176/p/14361893.html
已經開發好了,但是直接使用daphne運行,會遇到以下錯誤:
比如1:
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
比如2:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
解決方法
修改asgi.py,增加django.setup()
import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'websocket_demo.settings') django.setup() from channels.auth import AuthMiddlewareStack from django.core.asgi import get_asgi_application # Import other Channels classes and consumers here. from channels.routing import ProtocolTypeRouter, URLRouter # from apps.websocket_app.urls import websocket_urlpatterns from websocket_demo.urls import websocket_urlpatterns # application = get_asgi_application() application = ProtocolTypeRouter({ # Explicitly set 'http' key using Django's ASGI application. "http": get_asgi_application(), 'websocket': AuthMiddlewareStack( URLRouter( websocket_urlpatterns ) ), })
注意:django.setup()要置頂,不能在底部,否則使用daphne啟動會報上面的錯誤。
運行項目
注意:要在manage.py同級目錄下執行此命令
daphne websocket_demo.asgi:application -b 0.0.0.0 -p 8000
三、nginx+daphne+supervise
官方文檔:https://channels.readthedocs.io/en/stable/deploying.html#configuring-the-asgi-application
根據官方文檔,推薦使用nginx+daphne+supervise
環境說明
操作系統:centos 7.5
ip地址:192.168.31.165
supervise
yum install -y supervisor
生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
修改配置文件/etc/supervisord.conf,最后一行增加
[include] files = supervisord.d/*.ini
表示配置文件讀取supervisord.d目錄下所有后綴為.ini的文件。
創建配置目錄,並創建配置文件
mkdir /etc/supervisord.d/ vi /etc/supervisord.d/asgi.ini
內容如下:
[fcgi-program:asgi] # TCP socket used by Nginx backend upstream socket=tcp://localhost:8000 # Directory where your site's project files are located directory=/tmp/internal_tools # Each process needs to have a separate socket file, so we use process_num # Make sure to update "mysite.asgi" to match your project name command=/virtualenvs/venv1/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers internal_tools.asgi:application # Number of processes to startup, roughly the number of CPUs you have numprocs=4 # Give each process a unique name so they can be told apart process_name=asgi%(process_num)d # Automatically start and recover processes autostart=true autorestart=true # Choose where you want your log to go stdout_logfile=/var/log/asgi.log redirect_stderr=true
注意:紅色部分,請根據實際情況修改。
啟動supervisord
supervisord -c /etc/supervisord.conf
查看asgi運行狀態
# supervisorctl asgi:asgi0 RUNNING pid 17567, uptime 0:00:04 asgi:asgi1 RUNNING pid 17566, uptime 0:00:04 asgi:asgi2 RUNNING pid 17569, uptime 0:00:04 asgi:asgi3 RUNNING pid 17568, uptime 0:00:04
可以看到,有4個進程。如果狀態不是RUNNING,請查看ini配置文件,是否正常。
nginx
nginx安裝就很簡單了,一條命令解決
yum install -y nginx
修改虛擬配置文件/etc/nginx/conf.d/asgi.conf
upstream channels-backend { server localhost:8000; } server { listen 8093; location / { try_files $uri @proxy_to_app; } location @proxy_to_app { 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; } }
注意紅色部分,upstream 是asgi的監聽端口。在server里面的8093是對外端口,也可以改成別的,根據實際情況而定。
最后加載nginx配置文件
nginx -s reload
擴展
如果是前后端分離架構,在vue代碼中,配置nginx的服務器地址即可,比如:
Vue.prototype.$apihost = "http://192.168.31.165:8093" Vue.prototype.$websockethost = "ws://192.168.31.165:8093"
注意:daphne不光可以處理asgi,它也可以處理wsgi,沒有必要部署uswgi來處理wsgi了。
總之:nginx+daphne+supervise就可以處理django的所有功能了。
本文參考鏈接:
https://stackoverflow.com/questions/53683806/django-apps-arent-loaded-yet-when-using-asgi
https://docs.djangoproject.com/zh-hans/3.1/howto/deployment/asgi/daphne/
https://blog.csdn.net/qq_41854273/article/details/89332836
https://www.cnblogs.com/chenjw-note/p/12516097.html