Deploy Django application on Nginx and gunicorn : • Django 2.0 Deployment with Gunicorn, ...
To get your websockets running live (guaranteed) in 5 simple steps.
We setup NGINX, supervisor, daphne, asgi.py and redis to make sure it works.
If your WebSockets are running fine in local but you are having trouble with going live in production, this video is for you.
Building REST APIs over HTTP has been discussed time and again. But could we do the same with WebSockets?
Files:
Nginx setup file
My nginx conf file is
upstream app_server {
server unix:/home/urban/run/gunicorn.sock fail_timeout=0;
}
server {
add here the ip address of your server
or a domain pointing to that ip (like example.com or 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;
location /static/ {
alias /home/urban/homet_dj/static/;
}
checks for static file, if not found proxy to app
location / {
try_files $uri @proxy_to_app;
}
location /ws/ {
try_files $uri @proxy_to_ws;
}
location @proxy_to_ws {
proxy_pass http://0.0.0.0:8001;
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;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
Supervisor.conf file
[program:homet_dj]
command=/home/urban/bin/gunicorn_start
user=urban
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/urban/logs/gunicorn-error.log
[program:serverinterface]
command= daphne -b 0.0.0.0 -p 8001 homet_dj.asgi:application
directory=/home/urban/homet_dj/
autostart=true
autorestart=true
stopasgroup=true
user=urban
stdout_logfile = /home/urban/logs/gunicorn-error.log
websocket calling file:
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 = function(data) {
console.log('onclose');
console.log(data);
socket.onerror = function(data) {
console.log('error');
console.log(data);
}
}
Watch video Running Django-Channels and Websockets in Production | Setup Daphne and Supervisor online without registration, duration hours minute second in high quality. This video was added by user Ultimate Backend & AI 01 November 2018, don't forget to share it with your friends and acquaintances, it has been viewed on our site 15,327 once and liked it 214 people.