Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSDISCONNECT immedialty after connecting #972

Closed
tlebrize opened this issue Mar 12, 2018 · 5 comments
Closed

WSDISCONNECT immedialty after connecting #972

tlebrize opened this issue Mar 12, 2018 · 5 comments

Comments

@tlebrize
Copy link
Contributor

tlebrize commented Mar 12, 2018

I tried to make run the tutorial from the docs on my production server, using ssl.
After a few hours i managed to get a connection but it instantly disconnects :

None - - [12/Mar/2018:17:42:22] "WSCONNECTING /ws/chat/bibou/" - -
None - - [12/Mar/2018:17:42:22] "WSCONNECT /ws/chat/bibou/" - -
None - - [12/Mar/2018:17:42:23] "WSDISCONNECT /ws/chat/bibou/" - -

my stack is

ubuntu 16.04
nginx 1.10.3
channels==2.0.2
daphne==2.1.0
channels-redis==2.1.0
Twisted==17.9.0

I have the exact copy paste of the code from the tutorial, except for this part in room.html

    var chatSocket = new WebSocket(
        'wss://' + window.location.host +
        ':8443/ws/chat/' + roomName + '/');

and here is my nginx conf

server {
    #http
    listen 80;
    server_name domain.com;
    root /usr/share/nginx/html;
    include /etc/nginx/default.d/*.conf;

    location / {
        return 301 https://$server_name$request_uri;
    }
}

server {
    #https
    listen 443 ssl;
    listen 8443 ssl;
    server_name domain.com;
    root /usr/share/nginx/html;

    ssl_certificate "/etc/letsencrypt/live/domain.com/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/domain.com/privkey.pem";
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    add_header Strict-Transport-Security "max-age=31536000";

    include /etc/nginx/default.d/*.conf;

    location /static/ {
	root /home/ubuntu;
    }

    location /media/ {
        root /home/ubuntu;
    }

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://unix:/home/ubuntu/tlebrize/Projectsock;
    }
    
    location /ws/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade";

        proxy_pass http://unix:/home/ubuntu/tlebrize/Daphne.sock;
    }
}

I run daphne with daphne -u Daphne.sock Groover.asgi:application -v 3

I also tried bypassing nginx and using sudo daphne -e ssl:8443:privateKey=/etc/letsencrypt/live/groover.co/privkey.pem:certKey=/etc/letsencrypt/live/groove.co/fullchain.pem Groover.settings:CHANNEL_LAYERS -u Daphne.sock
but i had the same results.

The front break with the message Chat socket closed unexpectedly with the error code 1011 (Internal Error) and no reason.

@andrewgodwin
Copy link
Member

Does it work without SSL?

@matthiask
Copy link
Contributor

You should also set proxy_http_version 1.1;

@tlebrize
Copy link
Contributor Author

@andrewgodwin yes

@matthiask I tried that a bit later, still does'ntwork.

thanks

@andrewgodwin
Copy link
Member

Then it sounds like it might be a certificate problem or similar with your browser, given that it fails with both nginx and daphne? Is the certificate you're using issued for the right hostname and trusted? (the browser won't prompt you to skip a certificate if it's doing a websocket to a new port).

I'm also curious to know what you get in terms of scope/messages through to your consumer - would be good to add some logging there.

I don't think this is a channels problem, though, given that the Channels side successfully handshakes (as it gets to WSCONNECT), so I will close this ticket for now.

@tlebrize
Copy link
Contributor Author

I managed to make it work, it was an issue with nginx and/or using ReconnectingWebSocket. here's my whole working conf:
nginx

server {
    #http
    listen 80;
    server_name domain.co;
    root /usr/share/nginx/html;
    include /etc/nginx/default.d/*.conf;

    location / {
        return 301 https://$server_name$request_uri;
    }
}

server {
    #https
    listen 443 ssl;
    server_name domain.com;
    root /usr/share/nginx/html;

    ssl_certificate "/etc/letsencrypt/live/domain.com/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/domain.com/privkey.pem";
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    add_header Strict-Transport-Security "max-age=31536000";

    include /etc/nginx/default.d/*.conf;

    location /static/ {
	root /home/ubuntu;
    }

    location /media/ {
        root /home/ubuntu;
    }

    location /ws/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_pass http://127.0.0.1:8443;
    }
    
    location / {...}
}

daphne

sudo /home/ubuntu/venv/bin/daphne -e ssl:8443:privateKey=/etc/letsencrypt/live/domain.com/privkey.pem:certKey=/etc/letsencrypt/live/domain.com/fullchain.pem Project.asgi:application -v 3

js

    var chatSocket = new ReconnectingWebSocket(
        'wss://' + window.location.host +
        ':8443/ws/chat/' + roomName + '/');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants