Configuration for Django WebSocket Application on Ubuntu Server

Configuration for Django WebSocket Application on Ubuntu Server

This tutorial will walk you through each step in detail on how to configure your django websocket application on a  Ubuntu 20.10 server

This tutorial will walk you through each step in detail on how to configure your django websocket application on a Ubuntu 20.10 server. This article assumes you are familiar with Django and have a ubuntu remote server running up. To learn more about Django, checkout – Django Tutorial

First, let’s see what all we will be using to put this into production,

  1. Nginx – Web and Proxy Server
  2. Daphne – our ASGI (Asynchronous Server Gateway Interface) server which will serve our Django application
  3. Redis Backend Server – which will handle our web socket connections (ws://)

Nginx configuration

Install Nginx and Supervisor

$ sudo apt install nginx supervisor

In your /etc/nginx/sites-available/ folder create your server and add the below content, 

upstream redis_backend_server{
server localhost:6379;
}

upstream app_server {
server localhost:9090;
}

server {
listen 80;
listen 443 ssl;
keepalive_timeout 700;

ssl_certificate <path to your cert>;
ssl_certificate_key <path to your key>;

server_name foo.com www.foo.com;
access_log <path to your access logs>;
error_log <path to your error logs>;

add_header X-Frame-Options SAMEORIGIN;
add_header Content-Security-Policy "frame-ancestors self https://foo.com";

location /static/ {

root /var/www/staticfiles/;

  }

if ($scheme = http) {
       return 301 https://$server_name$request_uri;
  }

location / {
       include proxy_params;
       proxy_pass http://app_server;
  }

location /ws {
   proxy_pass http://redis_backend_server;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";

   proxy_ssl_certificate <path to your cert>;
   proxy_ssl_certificate_key <path to your key>;

   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;
   proxy_set_header X-Forwarded-Proto  $scheme;
   }
}

Note – Replace foo with your IP or your domain name.

python technical scripter python django

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Main Pros and Cons of Django As A Web Framework for Python Developers

India's best Institute for Django Online Training Course & Certification. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.