1

I have a socket.io webservice running port 8080 on my production server, it responds to http requests, but I think its having difficulty resolving the proxy when my client is sending over websocket protocols (ws://)

My client is telling me that the server is responding with a 400 (bad request) error, so something is either wrong on my client side, or my production server. Im banking on it being my production server, but neither myself or a co worker of mine can figure out where for sure.

These are the nginx configurations we have for our node.js production box.

I have replaced the real url with someapp.com

NGINX

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name app.someapp.com;

        location / {
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        Host            $http_host;
                proxy_pass              http://127.0.0.1:3000;
        }
}

server {
        listen 8080;
        server_name app.someapp.com;
        location / {
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        Host            $http_host;
                proxy_pass              http://127.0.0.1:4000;
        }
}

server {
        listen 80;
        server_name tools.someapp.com;
        root /var/www/bbclient/dist;
        location / {
                try_files $uri $uri/ /index.html;
        }
}

its a very small and simple socket.io node.js server running on port 8080 in production

Socket.IO

const app = require('express')()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

// this is not responding from the client
// this is working on localhost, but not in production
io.on('connection', (socket) => {
  socket.on('USER_ID', (userId) => {
    if (socket.userId) {
      socket.leave(socket.userId)
    }
    socket.userId = userId
    socket.join(userId)
  })
})

// this works over http, and responds in production
// by visiting http://app.someapp.com:8080 in the browser
app.get('/', (req, res) => {
  res.send('some app') 
})

http.listen(process.env.PORT || '8888', () => {
  console.log('listening')
})

Here is what the network tab is saying about the request coming from my client resulting in a 400 error.

chrome browser network tab

enter image description here

Edit 1

NGINX error logs:

2018/02/06 17:30:39 [error] 5954#5954: *86956 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 76.218.92.156, server: app.someapp.com, request: "GET /socket.io/?EIO=3&transport=polling&t=M5iAdrw&sid=Jays0pqU3StUhdjjAACb HTTP/1.1", upstream: "http://127.0.0.1:4000/socket.io/?EIO=3&transport=polling&t=M5iAdrw&sid=Jays0pqU3StUhdjjAACb", host: "someapp.com:8080", referrer: "http://tools.someapp.com/scorecards/estimating"
3
  • Have you checked the nginx error logs? Commented Feb 6, 2018 at 17:33
  • @MehdiElFadil I had not, but I just updated the post to include it, its timing out :( Commented Feb 6, 2018 at 18:00
  • The timeouts may be occuring because some error happens at node.js side, and the response is ever sent. What do your node.js logs say? Commented Feb 6, 2018 at 18:09

1 Answer 1

1

First of all add socket.io proxy directives:

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

Here's an example. Then try to increase proxy connection timeout:

    proxy_connect_timeout 75s;
    proxy_read_timeout 75s;
    proxy_send_timeout 75s;
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't the first set of proxy directives listed here only relevant for https?
Actually not. You can use it with http/https.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.