Nginx proxy_pass与节点和SSL

时间:2018-10-25 21:34:35

标签: node.js ssl nginx

通过将证书和密钥路径放置在Nginx conf文件中,我设法在localhost:4000的节点服务器上使用Nginx设置HTTPS,但是,我也想了解如何将ssl证书嵌入到节点index.js文件,只需将代理的地址以及应用程序内部的证书而不是localhost:4000文件传递到nginx.conf

一旦未将节点用作ssl,此nginx.conf文件将与https / ssl一起正常使用。

server {
  listen 4500 ssl;
  server_name example.com;

  ssl_certificate           /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key       /etc/letsencrypt/live/example.com/privkey.pem;

  access_log            /var/log/nginx/example.com.access.log;

  location / {
    proxy_pass http://localhost:4000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

但是,如果我注释掉以下nginx.conf

#    ssl_certificate           /etc/letsencrypt/live/api.ballers.ie/fullchain.pem;
#    ssl_certificate_key       /etc/letsencrypt/live/api.ballers.ie/privkey.pem;

然后按如下所示将证书添加到我的节点服务器,它告诉我无法访问该站点。

// ...
// dependencies & config
// ...

const privateKey = fs.readFileSync('/etc/letsencrypt/live/example.com.privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/example.com.cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/example.com.chain.pem', 'utf8');

const credentials = {
  key: privateKey,
  cert: certificate,
  ca,
};

const httpsServer = https.createServer(credentials, app);

app.get('*', (req, res) => {
  res.send('I am a HTTPS endpoint !');
});

httpsServer.listen(4000);

我还尝试将proxy_pass更改为proxy_pass https://localhost:4000;

感谢您的帮助,我非常想了解如何进行这项工作。

0 个答案:

没有答案