0

In our Django project, we are using django channels with wss socket connection. While sending data via socket it is not working when deployed in AWS ec2 instance (Ubuntu nginx/1.18.0) proxy server. Given below error:

chat/:1211 WebSocket connection to 'wss://chatbot.abc.com/ws/chat/room1/' failed: i n aws ec2 instance as docker image 2 / 2

Why is that so?

1 Answer 1

0

The question is not very detailed, but the answer lies in the usage of Daphne with Django Channels. Daphne works between your nginx server code and your python application to direct traffic where it needs to go.

Our websocket in this instance is running local to our server. Unlike other setups, we are not contacting another server to connect to this websocket, but rather a port on our local system. However, as you seem to be aware, we need to use wss and https on an online production server rather than ws and http. Because our websocket is running locally to our server, our system recognizes it as a ws connection rather than a wss connection. Therefore, we need to redirect ws to wss in your nginx server configuration.

There are many different types of web servers that can be used with Django/Django Channels. You will have to figure out how to do this in your configuration. There are some decent answers here to get you started: Nginx Reverse Proxy Websockets

Here is an example of how I have solved the problem in Apache:

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:8001%{REQUEST_URI} [P,QSA,L]
ProxyPass /wss/ wss://127.0.0.1:8001/
ProxyPassReverse /wss/ wss://127.0.0.1:8001/
...
SSLEngine on
SSLCertificateFile /etc/ssl/certificate.crt
SSLCertificateKeyFile /etc/ssl/private/private.key
SSLCertificateChainFile /etc/ssl/ca_bundle.crt

In production you also need an SSL certification which to ensure an encrypted connection, and you will need to specify that in your daphne instruction.

daphne -b 0.0.0.0 -p 8001 django_project.asgi:application // Local Development Level
daphne -e ssl:443:privateKey=key.pem:certKey=crt.pem django_project.asgi:application // Production Level
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.