使用nginx、nodejs时Mongodb服务器选择超时

yhived7q  于 2023-01-29  发布在  Nginx
关注(0)|答案(2)|浏览(147)

我挣扎在我的网页应用程序的https设置。它看起来像我已经成功了。
但是,当mongodb与nodejs连接时出现问题。
我猜nginx https反向代理设置造成这个麻烦,但,我不知道在哪里以及如何处理.
如果你能帮助我,我将非常感激。
环境

  • Ubuntu 18.04.3语言版本
  • Nginx 1.14.0
  • 蒙古数据库4.2.3
  • NodeJS + Koa
  • 自动气象站EC2

问题

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

  • 来自nodejs的错误消息

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

  • 来自nginx的错误消息

[错误] 14495#14495:* 230328从上游读取响应标头时,上游过早关闭连接,客户端: www.example.com,服务器:www.example.com,请求:"获取/获取团购价格?目标= UNION &日期选择= 2019 - 12 - 10 HTTP/1.1",上游:210.218.178.27"www.example.com",引用者:songistock.net, request: "GET /GetGroupPrice?target=UNION&dateOpt=2019-12-10 HTTP/1.1", upstream: " http://127.0.0.1:3000/GetGroupPrice?target=UNION&dateOpt=2019-12-10 ", host: "www.songistock.net", referrer: " https://www.songistock.net/ "

我试过

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

设置和代码

  • /etc/nginx/可用站点
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 });

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

yduiuuwa

yduiuuwa1#

我认为这是因为nginx主要是一个HTTP服务器,所以当你重定向到Mongo时,它会尝试使用http协议,而Mongo却期望一个原始的TCP连接。你应该尝试用stream块 Package 你的serverupstream块。
编辑:stream需要与http块处于同一级别。您也可以在http块之外为/etc/nginx/conf.d/*.conf创建include。

http{}
stream {

upstream stream_mongo_backend {
   server localhost:27017;
   }

server {
   listen 27017;

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

jjjwad0x2#

最佳解决方案:
您不需要处理多个.conf文件。需要在app.js或index.js运行目录中创建Dockerfile。
使用Docker构建服务器,并将多个端口分配给localhost作为转发代理。

user root;
worker_processes auto;
pid/run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}
 
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    upstream backend{
      server  http://127.0.0.1:11111;
      server  http://127.0.0.1:22222;
      server  http://127.0.0.1:33333;
      server  http://127.0.0.1:44444;
    }

    server{
       listen 80;
       server_name conspeer.in;
       root /conspeer_new;

       rewrite ^/(./$) /$1;

       location / {
           proxy_pass http://backend/;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection $http_connection;
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
      }
    }

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
   
    gzip on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;

   }

相关问题