14

I'm new to front end web app development. I'm receiving a WebSocket connection failure as follows:

WebSocket connection to 'ws://127.0.0.1:7983/websocket/' failed: Error in connection establishment: net::ERR_EMPTY_RESPONSE

I looked up this WebSocket error and found diverted to the following pages.

Shiny & RStudio Server: "Error during WebSocket handshake: Unexpected response code: 404"

WebSocket connection failed with nginx, nodejs and socket.io

Rstudio and shiny server proxy setting

I then downloaded nginx on my Windows 7 machine and added the following comment in nginx.conf, saved and executed runApp().

location /rstudio/ {
 rewrite ^/rstudio/(.*)$ /$1 break;
 proxy_pass http://localhost:7983;
 proxy_redirect http://localhost:7983/ $scheme://$host/rstudio/;
}

This didn't seem to solve the issue. I think I may need to add some extra stuff into the nginx.conf file or put it in a specific directory. Please assist. Thanks!

EDITED the nginx.conf script as follows:

        location /rstudio/ {
    rewrite ^/rstudio/(.*)$ /$1 break;
    proxy_pass http://127.0.0.1:5127;
    proxy_redirect http://127.0.0.1:5127/ $scheme://$host/rstudio/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }
Community
  • 1
  • 1
Aks
  • 932
  • 2
  • 17
  • 32

2 Answers2

13

I think you forgot these three wonderful lines of code required to use WebSockets with Nginx:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Add them to your location /rstudio/ {}

Also, by default the connection will be closed after 30 seconds without activity. Workaround:

proxy_read_timeout 999999999;

WebSockets need HTTP 1.1 protocol for working. These 3 lines make the browser connect to the website using HTTP 1.1, and proxy your server as HTTP 1.1.

If you want to know more, here's a blog post that might help.

Gabriel Tomitsuka
  • 1,121
  • 10
  • 24
  • It's still not working for me. Here's what I have in nginx.conf (edited my question). Should nginx.conf have a specific directory ? or how do I check if this is actually communicating with my browser ? Thanks! – Aks Apr 16 '15 at 15:18
  • Wait wait wait wait wait wait... WebSocket connection to 'ws://127.0.0.1:7983/websocket/' failed... Try 'ws://127.0.0.1:7983/rstudio/websocket/' instead. – Gabriel Tomitsuka Apr 16 '15 at 18:21
  • I have mentioned location /rstudio in the conf file. What exactly are you stating ? – Aks Apr 16 '15 at 22:20
  • I meant that the WS is being proxied under the /rstudio path. (Sorry for taking that long) – Gabriel Tomitsuka Jun 03 '15 at 14:15
5

After struggling on this same issue for some days I found that the problem was that the Firewall was preventing the websocket from working. I had Pandas Antivirus installed and Firewall was enabled in it. When I turned it off and used Windows firewall and opened that incoming port then it started working.

Hope it helps

voidrus
  • 78
  • 3