3

Using the development version of nginx (1.3.12). In my relevant file under sites-enabled:

upstream twisted {
    server 127.0.0.1:8088;
}

server {  

    listen   80; ## listen for ipv4  

    server_name  *.clurn.co.uk clurn.co.uk;  

    access_log  /var/log/nginx/clurn.co.uk.access.log;  

    location / {  
            root   /var/www/clurn.co.uk;  
            index  index.html index.htm;  

    }  

    location /websocket {  
            proxy_pass http://twisted;  
            proxy_http_version 1.1;  
            proxy_set_header Upgrade $http_upgrade;  
            proxy_set_header Connection "Upgrade";  
    }  

    location /websocket/server {  
            deny all;  
    }  

    [...snip...]
}

Only difference from the example on nginx's site is the capital u on upgrade, which seemed to be necessary for the Autobahn and/or txWS instances to recognise the websockets correctly (I can't remember which one it was atm).

Now, if I access the websockets via 127.0.0.1:8088 on the server, then it appears to work, but if I access it via 127.0.0.1/websockets it doesn't, so there has to be something wrong with the marshalling of the packets via nginx.

If I run netcat -l -p 8088 instead of the Twisted websocket backend, I can see that the requests are identical if accessed via nginx or directly (originally they weren't, but changing the "upgrade" to "Upgrade" made the header line Connection: Upgrade the same), so this part is proxying correctly.

However, if I telnet into 127.0.0.1:8088 and 127.0.0.1/websocket, copying and pasting the correct request, I get a response with the direct 127.0.0.1:8088, and do not with the 127.0.0.1/websocket. What am I doing wrong, and how can I set it up to allow websockets on both sides?

socksy
  • 33
  • 1
  • 3
  • 127.0.0.1 **/websockets** and 127.0.0.1:8088 **/** are different requests. Is it still working when you request 127.0.0.1:8088 **/websockets**? – VBart Feb 28 '13 at 20:39
  • 2
    Did you solve this? I'm getting pretty much the same behavior. I was able to successfully proxy through nginx with a websocket-only server (all the set_headers being at the root level of the server config), but get no responses otherwise. – outofculture Mar 22 '13 at 18:11

1 Answers1

4

I think the problem is buffering. Set:

proxy_buffering off;

inside the websocket location config.

outofculture
  • 156
  • 3
  • Unfortunately I no longer have the setup so can't check. But it sounds like if it solved your issue (if it were the same) then it may as well be marked as correct. – socksy Nov 08 '13 at 03:12
  • Another option is to disable buffering for just the upgrade with `proxy_cache_bypass $http_upgrade;`, may need to ensure upgrade options set `proxy_set_header Upgrade $http_upgrade;` and `proxy_set_header Connection 'upgrade';`. Upside is this disables buffering for just the websocket connection if your websocket location is same as regular http/https location. – Cymen Mar 20 '22 at 19:21