在Nginx和Gunicorn上部署Django應用程序:https://www.youtube.com/watch?v=08yYjLGWzaM&t=942s
通過5個簡單的步驟即可使Websocket實時運行(保證)。
我們設置NGINX,supervisor,daphne,asgi.py和redis以確保其正常工作。
如果您的WebSockets在本地運行良好,但在上線生產時遇到了麻煩,則此視頻適合您。
通過HTTP構建REST API的問題已經被反覆討論。但是我們可以對WebSockets做同樣的事情嗎?
檔案:
Nginx安裝文件
我的nginx conf文件是
上游app_server {
伺服器Unix:/home/urban/run/gunicorn.sock fail_timeout = 0;
}
伺服器{
#在此處添加伺服器的IP地址
#或指向該IP的域(例如example.com或www.example.com)
server_name YOUR_IP;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/urban/logs/nginx-access.log;
error_log /home/urban/logs/nginx-error.log;
位置/ static / {
別名/ home / urban / homet_dj / static /;
}
#檢查靜態文件,如果找不到,則為應用程序代理
位置 / {
try_files $ uri @proxy_to_app;
}
位置/ ws / {
try_files $ uri @proxy_to_ws;
}
位置@proxy_to_ws {
proxy_pass http://0.0.0.0:8001;
proxy_http_version 1.1;
proxy_set_header升級$ http_upgrade;
proxy_set_header連接「升級」;
proxy_redirect關閉;
proxy_set_header主機$ host;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
proxy_set_header X轉發的主機$ server_name;
}
位置@proxy_to_app {
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
proxy_set_header主機$ http_host;
proxy_redirect關閉;
}
Supervisor.conf文件
[program:homet_dj]
命令= / home / urban / bin / gunicorn_start
用戶=城市
autostart = true
autorestart = true
redirect_stderr = true
stdout_logfile = / home / urban / logs / gunicorn-error.log
[program:serverinterface]
命令= daphne -b 0.0.0.0 -p 8001 homet_dj.asgi:應用程序
目錄= / home / urban / homet_dj /
autostart = true
autorestart = true
stopasgroup = true
用戶=城市
stdout_logfile = /home/urban/logs/gunicorn-error.log
websocket調用文件:
var socket = new WebSocket(「 wss://YOUR_IP.com/ws/notify」)
socket.onopen = function(){
console.log(’connected’);
console.log(’connected’);
}
socket.onmessage = function(data){
console.log(’onmaeesge’);
console.log(data);
}
socket.onclose =函數(數據){
console.log(’onclose’);
console.log(data);
socket.onerror =函數(數據){
console.log(’錯誤’);
console.log(data);
}
}。
Can you tell me resources from where you got this information? This is very interesting and I want to read more about deploying my Django app that uses Channels to production
ws/notify" ws required ?
Can u explain how to use apache and daphne together
I'm getting this error:
serverinterface: ERROR (no such file)
Please help me!!!
How can I access var/log/gunicorn-error.log?
I try to view the file to debug the "Excited too quickly"
nice sir tq so much sir
Very nice sir , you are doing very well
Thank you for this very useful lecture