2

I am trying to create a websocket using Nginx as a reverse proxy and nodejs at the back. I'm using ws library in nodejs. When I test it using wscat tool everything works fine but as I make request from browser, I'm continuously receiving 400 : Bad Request response from Nginx. I cannot find anything over internet with the error, nginx and nodejs together(might be my bad). I did exactly as mentioned in the tutorial.

Following is my node configuration-

console.log("Server started");
var Msg = '';
var WebSocketServer = require('ws').Server
    , wss = new WebSocketServer({port: 8010});
    wss.on('connection', function(ws) {
        ws.on('message', function(message) {
        console.log('Received from client: %s', message);
        ws.send('Server received from client: ' + message);
    });
 });

Nginx conf-

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server 127.0.0.1:8010;
    }

    server {
        listen 8020;
        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

Then I tried to work with socket.io from this tutorial. But then I am confused how to deliver index.html file containing

var socket = io(); // your initialization code here.

to browser as I want only specific request make web socket. I don't even understand how is it going to find socket.io.js as I don't find in where mentioned.

Please help.

Satys
  • 183
  • 1
  • 2
  • 11
  • Check your logs. – Michael Hampton May 15 '16 at 12:08
  • I did check them and I don't see any errors there. :( And one thing to mention is that when I stop node, I do get 502, that hints it does reach to node, so I assume the error is somewhere in node configuration. – Satys May 15 '16 at 12:30
  • You need to change the log level to `info` to obtain useful information about the 400 response. See [this document](http://nginx.org/en/docs/ngx_core_module.html#error_log) for details. – Richard Smith May 15 '16 at 16:09
  • I did that but it's giving a lot of info for single request and it's really difficult to understand that. – Satys May 15 '16 at 16:14

0 Answers0