简体   繁体   中英

socket.io + nginx + proxy problems

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

在此处输入图片说明

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"

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM