3

I am trying to setup NGINX to proxy web socket traffic. I am running a web page on NGINX (port 80) that has an MJPEG feed from port 8080 and also takes web socket traffic over port 8090. I can proxy the MJPEG stream, but not the web sockets. In my webpage, client side javascript was connecting fine without the proxy using this line:

var conn = new WebSocket('ws://192.168.0.14:8989/ws');

To proxy this I tried setting up the following NGINX config:

    #Proxy the Web Socket Traffic
    #----------------------------------------------
    location /sock/ {
        proxy_pass ws://localhost:8989/ws;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    #Proxy the MJPEG Streamer feed
    #----------------------------------------------
    location /mjpeg/ {
            proxy_pass http://localhost:8080/;
    }

And then changed the client side javascript to the following:

var conn = new WebSocket('http://192.168.0.14/sock/');

However when I restart of NGINX results in:

Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.

If I change the proxy pass line to "proxy_pass http://localhost:8989/ws;" then the NGINX service restarts successfully but I can make a web socket connection anymore.

Is there anything else I need to configure so that NGINX will proxy to ws:// addresses? Is http:// equivalent if it is upgrading the protocol? If the latter is true, then why doesnt it work?

I notice in this post that they are using ws:// in the proxy pass statement. But it doesnt work for me NGINX: How to proxy http(s) traffic to one server and ws(s) traffic to another?

user383341
  • 31
  • 1
  • 1
  • 2
  • What does nginx error log show? – Tero Kilkanen Oct 30 '16 at 01:21
  • This is an answer by @lee-melbourne who posted it by editing the question itself... Solution: So I now have this working. You still need to use ws:// in the client side javascript to connect. NGINX will still pick that connection up and proxy it. So the client side js is: `var conn = new WebSocket('ws:///sock/ws');` And the proxy config is: `location /sock/ { proxy_pass http://localhost:8989/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }` – Pothi Kalimuthu Nov 05 '16 at 01:40
  • @lee-melbourne ... please post it as an answer rather than editing the question. Thanks. – Pothi Kalimuthu Nov 05 '16 at 01:40

1 Answers1

3

No nginx documentation available mentions the ws:// scheme as available to use in the config. No wonder the nginx fails to start, I bet it's the configuration error it's complaining about.

And you definitely have the correct configuration for the websockets proxy. As about why it doesn't work - it's still up to determining, this can be due to large set of reasons: nobody really listens on the tcp/8989, or some application error in the listener happens.

drookie
  • 8,051
  • 1
  • 17
  • 27