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.