如何在 Nginx 反向代理后面设置 MongoDB [英] How to setup MongoDB behind Nginx Reverse Proxy

查看:106
本文介绍了如何在 Nginx 反向代理后面设置 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Nginx 设置为访问 MongoDB 数据库的反向代理.默认情况下,Mongo 侦听 27017 端口.我想要做的是通过 nginx 重定向主机名,例如 mongodb.mysite.com 并将其传递给 mongodb 服务器.通过这种方式,我将从外部网络关闭已知的 27017 端口,并从我给出的示例中的隐藏 url 访问我的数据库.

I am trying to setup Nginx as a reverse proxy for accessing a MongoDB Database. By default Mongo listens to 27017 port. What I want to do, is redirect a hostname for example mongodb.mysite.com through nginx and pass it to mongodb server. In that way from the outside network I will have my known 27017 port closed, and access my db from a hidden url like the example I gave.

所以我正在尝试使用此配置设置 Nginx :

So I am trying to setup Nginx with this configuration :

server {
        listen 80;
        server_name mongo.mysite.com;
        gzip off;       

        location / {
            proxy_pass http://127.0.0.1:27017;
            proxy_redirect off;
            proxy_buffering off;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

因此,在完成此操作后,我尝试使用命令 mongo mongo.mysite.com:80 从我的 cmd 连接 mongo shell,然后我返回以下错误:

So after having this I try to connect with mongo shell from my cmd with the command mongo mongo.mysite.com:80 and I get back the following error:

2015-08-06T13:44:32.670+0300 I NETWORK  recv(): message len 1347703880 is invalid. Min 16 Max: 48000000
2015-08-06T13:44:32.670+0300 I NETWORK  DBClientCursor::init call() failed
2015-08-06T13:44:32.674+0300 E QUERY    Error: DBClientBase::findN: transport error: mongo.therminate.com:80 ns: admin.$cmd query: { whatsmyuri: 1 }
    at connect (src/mongo/shell/mongo.js:181:14)
    at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed

也在 Nginx 访问日志中我得到了这个:

Also in the Nginx access log I get this:

94.66.184.128 - - [06/Aug/2015:10:44:32 +0000] "<x00x00x00x00x00x00x00x00x00x00x00xD4x07x00x00x00x00x00x00admin.$cmdx00x00x00x00x00x01x00x00x00x15x00x00x00x10whatsmyurix00x01x00x00x00x00" 400 172 "-" "-"

有没有人知道,这里出了什么问题?谢谢!

Has anyone got an idea, what is going wrong here? Thanks!

推荐答案

我把这个抛在了脑后,但做了一些工作后,我不得不再次面对这个问题,这次我想到了解决方案!

I left this behind, but after some work done, I had to face this problem again and the solution popped in my mind this time!

NGINX 基本上是一个 HTTP 服务器,因此通过上述方式设置重定向和代理,它将所有通信包装在 HTTP 协议中.所以发生的错误是,虽然 Mongo 期待原始 TCP 流量,但它正在获取 HTTP 流量.

NGINX is basically an HTTP server, so by setting redirects and proxies the above way, it wraps all communication in HTTP protocol. So the error that is happening, is that while Mongo is expecting Raw TCP traffic, it is getting HTTP traffic.

因此,解决方案是使用 NGINX 的新 stream 模块,该模块用于处理原始 TCP 流量并将上游服务器设置为指向 mongodb 实例.

So the solution to this is to use NGINX's new stream module that is used for handling raw TCP traffic and setup your upstream server to point to mongodb instance.

更多信息:NGINX 流模块

这篇关于如何在 Nginx 反向代理后面设置 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆