【问题标题】:socket.io + nginx + proxy problemssocket.io + nginx + 代理问题
【发布时间】:2018-02-06 17:24:07
【问题描述】:

我的生产服务器上有一个运行端口 8080 的 socket.io web 服务,它响应 http 请求,但我认为当我的客户端通过 websocket 协议 (ws://) 发送时它难以解析代理

我的客户告诉我服务器正在响应 400(错误请求)错误,因此我的客户端或生产服务器出现问题。我希望它成为我的生产服务器,但我自己或我的同事都无法确定在哪里。

这些是我们的 node.js 生产箱的 nginx 配置。

我已经用 someapp.com 替换了真实的 url

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;
        }
}

它是一个非常小而简单的 socket.io node.js 服务器,在生产中运行在端口 8080 上

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')
})

这是网络选项卡中关于来自我的客户端的请求导致 400 错误的内容。

chrome 浏览器网络标签

编辑 1

NGINX 错误日志:

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"

【问题讨论】:

  • 你检查过nginx错误日志吗?
  • @MehdiElFadil 我没有,但我刚刚更新了帖子以包含它,它的超时:(
  • 可能会发生超时,因为 node.js 端发生了一些错误,并且响应已发送。你的 node.js 日志是怎么说的?

标签: node.js nginx socket.io


【解决方案1】:

首先添加socket.io代理指令:

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

这是example。 然后尝试增加代理连接超时:

    proxy_connect_timeout 75s;
    proxy_read_timeout 75s;
    proxy_send_timeout 75s;

【讨论】:

  • 这里列出的第一组代理指令不是只与https相关吗?
  • 其实不是。您可以将其与 http/https 一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多