简单介绍下: websocket的主要协议ws/wss,类似于http/https的关系,如果使用了https那就必须使用wss协议。
首先django应用启动ws服务,
启动runworker python manage.py runworker 具体实现处理的进程 ,可以理解为干活的
启动daphne daphne -b 0.0.0.0 -p 8888 {应用名}.asgi:channel_layer 端口监听进程,理解为看门的。
如果没报错,系统就启动了8888端口用来处理websocket请求
nginx配置, 这里将https和wss请求放一个端口,使用后缀区分,这里展示一个https/wss的配置
点击(此处)折叠或打开
-
server {
-
listen 8010 ssl;
-
server_name api.test.com;
-
root /usr/share/nginx/html;
-
-
ssl_certificate "api.test.com.pem"; #加入证书配置
-
ssl_certificate_key "api.test.com.key";
-
ssl_session_cache shared:SSL:1m;
-
ssl_session_timeout 10m;
-
ssl_ciphers HIGH:!aNULL:!MD5;
-
ssl_prefer_server_ciphers on;
-
-
-
location / { #django后端代理
-
include uwsgi_params;
-
uwsgi_pass 127.0.0.1:8000;
-
client_max_body_size 35m;
-
}
-
-
location /ws { #wss代理配置,主要设置http协议header支持websocket
-
proxy_pass http://127.0.0.1:8888;
-
proxy_http_version 1.1;
-
proxy_set_header Upgrade $http_upgrade;
-
proxy_set_header Connection "upgrade";
-
}
- }