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

Key error: wsgi.websocket #29

Closed
nskrypnik opened this issue May 8, 2012 · 4 comments
Closed

Key error: wsgi.websocket #29

nskrypnik opened this issue May 8, 2012 · 4 comments

Comments

@nskrypnik
Copy link

Hi, all.

When I try to open websocket from chrome/ff via nginx proxy it fails with key error you might look in traceback

127.0.0.1 - - [2012-05-08 03:39:23] "GET /socket.io/1/websocket/896374484054 HTTP/1.1" 500 403720 0.108159
Traceback (most recent call last):
File ".../eggs/gevent-0.13.7-py2.6-linux-x86_64.egg/gevent/greenlet.py", line 390, in run
result = self._run(_self.args, *_self.kwargs)
File ".../eggs/gevent_socketio-0.3.5_beta-py2.6.egg/socketio/server.py", line 65, in handle
handler.handle()
File ".../eggs/gevent-0.13.7-py2.6-linux-x86_64.egg/gevent/pywsgi.py", line 180, in handle
result = self.handle_one_request()
File ".../eggs/gevent-0.13.7-py2.6-linux-x86_64.egg/gevent/pywsgi.py", line 314, in handle_one_request
self.handle_one_response()
File ".../eggs/gevent_socketio-0.3.5_beta-py2.6.egg/socketio/handler.py", line 123, in handle_one_response
jobs = self.transport.connect(socket, request_method)
File ".../eggs/gevent_socketio-0.3.5_beta-py2.6.egg/socketio/transports.py", line 215, in connect
websocket = self.handler.environ['wsgi.websocket']
KeyError: 'wsgi.websocket'

But then I try to connect with application directly it's ok and works fine. As you see nginx proxy module uses http/1.1 connection with my app server.

@dswarbrick
Copy link
Contributor

I've run into this issue with Apache's mod_proxy too. However, are you sure that your websocket will even work via Nginx's proxy module? Websockets require full-duplex communication, and I suspect that most proxies only support a single request/response pair.

@abourget
Copy link
Owner

abourget commented May 8, 2012

Exactly. Nginx doesn't forward all the headers (because it seems Websocket isn't fully HTTP/1.1 compliant) and it doesn't allow for full negotiation of the websocket. Thus, 'wsgi.websocket' isn't inserted in the environment (that's where you get your KeyError). Also, I don't think Nginx is ready to do full-duplex either.

I know it is possible to configure HAProxy to forward everything to the gevent server, so I would suggest looking in that direction.

Since this ticket isn't related to a bug in gevent-socket, I'm going to close it.

Hope this helps.

@laginha
Copy link

laginha commented Apr 4, 2013

Nginx supports websockets as you can see here. I've tested it in front of a simple Node.JS server and with Socket.io client. It worked!

Now, in a project i'm currently working on, I'm running Gunicorn + gevent-socketio as recommended in the docs. No problem there. However I've tried to set Nginx up with SSL enabled, like this:

server {
    listen      443;
    server_name  events.hostname;
    ssl on;
    ssl_certificate /path/to/x.crt;
    ssl_certificate_key /path/to/x.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout  5m;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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-Proto https;
        proxy_redirect off;
    }
}

Without success.
Every time my Socket.io client tries to connect, it receives a 500 status code due to the KeyError: 'wsgi.websocket' exception. I guessed the "Upgrade" header was missing, which I find weird.

So i changed the configuration a little bit and set the header manually proxy_set_header Upgrade "websocket". As a result the exception is raised no-more. However after a while i get this error Error during WebSocket handshake: 'Upgrade' header is missing on my javascript console.

Dispite this problem may not be related to a bug in gevent-socket, I would appreciate any help. Any ideias of what might be wrong? Am I missing anything?

@jensenw
Copy link

jensenw commented Sep 27, 2013

I use Gunicorn + nginx1.4.1 ,and add my nginx.conf

   location / {
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                   proxy_set_header Host $http_host;

                   proxy_redirect off;

                   proxy_pass http://127.0.0.1:8082/;
                   proxy_http_version 1.1;
                   proxy_set_header Upgrade $http_upgrade;
                   proxy_set_header Connection "upgrade";
                    }

when running the code, I keep get this error " KeyError: 'wsgi.websocket' " ,but application stil work and it's little bit slow when trying to connect websocket .

after googling then i'm installing nginx-full and my problem solved ,hope this can help.

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

No branches or pull requests

5 participants