Bearle / django-private-chat

(Deprecated - Please check out https://github.com/Bearle/django_private_chat2) Django one-to-one Websocket-based Asyncio-handled chat, developed by Bearle team
ISC License
424 stars 132 forks source link

Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR #68

Open ledzgio opened 3 years ago

ledzgio commented 3 years ago

Description

I am having issues in production where I am running nginx on HTTPS and uwsgi deployed on an EC2 instance.

In the browser console I get this error: WebSocket connection to 'wss://ec2-***:5002/session_id/username' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

What I Did

Here are my settings: CHAT_WS_SERVER_HOST = 'ec2-***' # here I am using EC2 Public IPv4 DNS CHAT_WS_SERVER_PORT = 5002 CHAT_WS_SERVER_PROTOCOL = 'wss'

I run the server as following: python manage.py run_chat_server

And in the server console I get:


`07.10.20 16:58:49:DEBUG:Invalid handshake
Traceback (most recent call last):
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/http.py", line 96, in read_request
request_line = await read_line(stream)
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/http.py", line 219, in read_line
raise EOFError("line without CRLF")
EOFError: line without CRLF
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/server.py", line 233, in read_http_request
path, headers = await read_request(self.reader)
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/http.py", line 98, in read_request
raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/server.py", line 134, in handler
extra_headers=self.extra_headers,
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/server.py", line 526, in handshake
path, request_headers = await self.read_http_request()
File "/home/admin/poc_platform/env/lib/python3.7/site-packages/websockets/server.py", line 235, in read_http_request
raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
07.10.20 16:58:49:DEBUG:server > HTTP/1.1 400 Bad Request

Any idea? thanks

ledzgio commented 3 years ago

If anyone is interested, I've managed to get it working on AWS.

Basically what I did is:

Edit the connection string in views.py as follows:

wss://domain.com/ws/session_id/username

Edit handlers.py, function main_handler to get username and session_id based on the new connection string defined above

Add this setting under nginx SSL server:

location /ws {
        proxy_pass http://chatserver;
        proxy_connect_timeout 7d;
        proxy_read_timeout 7d;
        proxy_send_timeout 7d;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
}

add the following outside the server directives:

upstream chatserver {
        server 127.0.0.1:5002  max_fails=1 fail_timeout=20s;
}

and here are the settings.py:

CHAT_WS_SERVER_HOST = '127.0.0.1'
CHAT_WS_SERVER_PORT = 5002
CHAT_WS_SERVER_PROTOCOL = 'wss'

Now start the wesocket server as following:

python manage.py run_chat_server

Hope this help those that had issues like me. Cheers.