title: How to configure nginx to proxy MongoDB daemon in ssl mode?
body: <p>I have configured MongoDB according to <a href="https://docs.mongodb.org/manual/core/security-transport-encryption/" rel="nofollow">https://docs.mongodb.org/manual/core/security-transport-encryption/</a> to use SSL. And it works fine.</p>
  
  <p>Now I need to place it behind the nginx.
  When I use following configuration:</p>
  
  <pre><code>server {
      listen       27017;
      server_name  mysite.com;
  
      location / {
          proxy_pass http://localhost:12345/;
          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_set_header Host $host;
          client_max_body_size 110m;
      }
  
      add_header  X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
  }
  </code></pre>
  
  <p>When I try to connect I have received:</p>
  
  <p>{com.mongodb.MongoException$Network: Exception opening the socket}, caused by 
  {javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?}</p>
  
  <p>So Ihave added nginx ssl directive:</p>
  
  <pre><code>server {
      listen       27017 ssl;
      server_name  mysite.com;
  
      location / {
          proxy_pass http://localhost:12345/;
          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_set_header Host $host;
          client_max_body_size 110m;
      }
  
      add_header  X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
  }
  </code></pre>
  
  <p>After that I have received:</p>
  
  <p>{com.mongodb.MongoException$Network: Exception opening the socket},  caused by 
  {javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake}, caused by 
  {java.io.EOFException: SSL peer shut down incorrectly}</p>
  
  <p>Can anyone help me please?</p>