【问题标题】:Mongodb server selection time out with nginx, nodejsMongodb服务器选择超时与nginx,nodejs
【发布时间】:2020-05-17 09:32:52
【问题描述】:

我在我的网络应用程序上遇到了 https 设置问题。看起来我已经成功了。

但是,mongodb与nodejs连接时出现问题。

我猜是 nginx https 反向代理设置会导致这个问题,但是, 我不知道在哪里以及如何接近。

如果你能帮助我,我将非常感谢你。

环境

  • Ubuntu 18.04.3 LTS
  • Nginx 1.14.0
  • MongoDB 4.2.3
  • NodeJS + Koa
  • AWS EC2

问题

Mongodb 在尝试连接时返回以下错误

  • 来自 nodejs 的错误消息

    服务器选择在 30000 毫秒后超时

  • 来自 nginx 的错误消息

    [错误] 14495#14495:*230328 上游过早关闭连接,同时从上游读取响应标头,客户端:210.218.178.27,服务器:songistock.net,请求:“GET /GetGroupPrice?target=UNION&dateOpt=2019-12- 10 HTTP/1.1”,上游:“http://127.0.0.1:3000/GetGroupPrice?target=UNION&dateOpt=2019-12-10”,主机:“www.songistock.net”,引用者:“https://www.songistock.net/

我试过了

  • 在 nginx 中设置上游
  • 重新启动 Ubuntu
  • 重新安装 Nginx、Mogodb

设置和代码

  • /etc/nginx/sites-available
upstream stream_mongo_backend {
   server localhost:27017;
}

server {
   listen 27017;

   location / {
      proxy_pass http://stream_mongo_backend;
   }
}

server {
    listen 80;
    return 301 https://$host$request_uri;
}

server {
 listen 443 ssl;
 server_name songistock.net www.songistock.net;

 ssl_certificate /etc/letsencrypt/live/www.songistock.net/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/www.songistock.net/privkey.pem;

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
 location / {
         proxy_pass http://127.0.0.1:3000;

         proxy_set_header        Host $host;
         proxy_set_header        X-Real-IP $remote_addr;
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header        X-Forwarded-Proto $scheme;
         proxy_read_timeout      120;
         proxy_http_version 1.1;
         proxy_redirect off;
        }
}

  • NodeJS 代码

此代码使用 https 设置

const Router = require('koa-router');
const router = new Router();
const mongoose = require('mongoose');

const winston = require('../logger/winston');

var db = mongoose.connection;
db.once('open', function () {
    winston.info("db connected");
});

mongoose.connect('mongodb://localhost:27017/songi_stock', 
{ useNewUrlParser: true,useUnifiedTopology: true });

如果需要更多信息。请评论。然后我将添加更多信息。 我真的很想解决这个问题。

感谢您的帮助。

【问题讨论】:

    标签: node.js mongodb nginx


    【解决方案1】:

    我认为这是因为 nginx 主要是一个 HTTP 服务器,所以当您重定向到 Mongo 时,它会尝试使用 http 协议,但 Mongo 需要一个原始的 TCP 连接。您应该尝试用 stream 块包装 Mongo 的 serverupstream 块。

    编辑:stream 需要与http 块处于同一级别。您还可以在 http 块之外为 /etc/nginx/conf.d/*.conf 添加包含。

    http{}
    stream {
    
    upstream stream_mongo_backend {
       server localhost:27017;
       }
    
    server {
       listen 27017;
    
       location / {
          proxy_pass http://stream_mongo_backend;
         }
       }
    }
    

    【讨论】:

    • 感谢您的回答。但是当我尝试它不起作用时。 nginx 说“流”指令是不允许的
    • 感谢您的编辑。我刚刚尝试了您编辑的方式,但现在 nginx 说“位置”指令是不允许的
    • 您是将include 放在http 块之外还是只将stream 放在它之外?尝试两种方式。
    • 谢谢!! @C.Gochev 我用你的编辑答案解决了问题并重新启动了 mongodb 服务。编辑是对的。 Mongodb 在测试您的答案时死了。所以看起来失败了。
    猜你喜欢
    • 2022-12-15
    • 2022-12-21
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2022-11-11
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多