programing

Nginx Reverse Proxy 뒤에 MongoDB를 설정하는 방법

sourcejob 2023. 6. 28. 21:41
반응형

Nginx Reverse Proxy 뒤에 MongoDB를 설정하는 방법

MongoDB 데이터베이스에 액세스하기 위한 역방향 프록시로 Nginx를 설정하려고 합니다.기본적으로 Mongo는 27017 포트를 수신합니다.제가 하고 싶은 것은 예를 들어 mongodb.mysite.com 의 호스트 이름을 nginx를 통해 리디렉션하여 mongodb 서버에 전달하는 것입니다.그런 식으로 외부 네트워크에서 알려진 27017 포트를 닫고 제가 제공한 예와 같이 숨겨진 URL에서 제 DB에 액세스합니다.

그래서 나는 다음과 같은 구성으로 Nginx를 설정하려고 합니다.

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

그래서 이것을 먹은 후에 나는 명령어로 cmd의 mongo shell에 연결하려고 합니다.mongo mongo.mysite.com:80다음 오류가 발생했습니다.

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 액세스 로그에서도 다음과 같이 표시됩니다.

94.66.184.128 - - [06/Aug/2015:10:44:32 +0000] "<\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xD4\x07\x00\x00\x00\x00\x00\x00admin.$cmd\x00\x00\x00\x00\x00\x01\x00\x00\x00\x15\x00\x00\x00\x10whatsmyuri\x00\x01\x00\x00\x00\x00" 400 172 "-" "-"

여기서 무슨 일이 일어나고 있는지 아는 사람 있나요?감사합니다!

맞습니다. .conf 파일에 스트림 섹션을 추가하여 NGINX의 스트림 모듈을 사용해야 합니다.

stream {
    server {
        listen  <your incoming Mongo TCP port>;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass    stream_mongo_backend;
    }

    upstream stream_mongo_backend {
      server <localhost:your local Mongo TCP port>;
  }
}

@Néstor의 답변에 추가하여, 이 구성은 다음에 작성되어야 합니다./etc/nginx/nginx.conf바로 위에http섹션, 다음과 같이:

stream {
    server {
        listen  <your incoming Mongo TCP port>;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass    stream_mongo_backend;
    }

    upstream stream_mongo_backend {
        server <localhost:your local Mongo TCP port>;
    }
}

http {
    ...
}

당신은 그것을 절대로 쓰지 말아야 합니다..conf파일을 정리하여 에 넣습니다./etc/nginx/sites-available폴더를 누릅니다.구성 정보가 있으면/etc/nginx/sites-available폴더는 에 속합니다.http부분.

저는 이것을 남겼지만, 몇 가지 일이 끝난 후, 저는 이 문제에 다시 직면해야 했고 이번에 해결책이 생각났습니다!

NGINX는 기본적으로 HTTP 서버이므로 위와 같이 리디렉션 및 프록시를 설정하면 HTTP 프로토콜에서 모든 통신을 래핑합니다.따라서 발생하는 오류는 Mongo가 원시 TCP 트래픽을 예상하는 동안 HTTP 트래픽을 수신한다는 것입니다.

따라서 이에 대한 해결책은 NGINX의 새 제품을 사용하는 것입니다.stream module원시 TCP 트래픽을 처리하고 mongodb 인스턴스를 가리키도록 업스트림 서버를 설정하는 데 사용됩니다.

추가 정보: NGINX 스트림 모듈

팁을 하나 더 공유하고 싶습니다.나는 mongodb에 대한 nginx ssl 지원을 방금 구성했습니다.그것은 더 안전해야 합니다.아래와 같은 구성을 볼 수 있습니다.

stream {
    server {
        listen  <your incoming port> ssl so_keepalive=on;
        ssl_certificate <your path to certificate>;
        ssl_certificate_key  <your path to private key>;
        proxy_connect_timeout 2s;
        proxy_pass stream_mongo_backend;
        proxy_timeout 10m;
    }

    upstream stream_mongo_backend {
         server 127.0.0.1:<your mongodb port>;
    }
}

다음을 사용하여 mongodb에 액세스하기 위해 파이썬 코드로 테스트했습니다.

mng_client = pymongo.MongoClient('mongodb://<your username>:<your password>@<ipaddress or domain name>:<your incoming port as defined above>/?tls=true')

일반적인 기본 IP 값을 통해 mongodb의 로컬 인스턴스에 연결하는 경우 연결해야 합니다.mongo 10.8.8.10

문제는 발생하지 않는 mongodb 쉘을 통해 주소를 해결하는 것입니다.

언급URL : https://stackoverflow.com/questions/31853755/how-to-setup-mongodb-behind-nginx-reverse-proxy

반응형