简体   繁体   English

如何配置 Nginx 和 Node 使用 SSL?

[英]How to configure Nginx and Node to use SSL?

I'm trying to deploy a Node app for the first time, and I have some doubts regarding ssl configuration as this is not my area of expertise.我是第一次尝试部署 Node 应用程序,我对 ssl 配置有一些疑问,因为这不是我的专业领域。

I'm using Sequelize to connect to a managed postgres db and every time I try to make a request, I get a "Self signed certificate in certificate chain" error.我正在使用 Sequelize 连接到托管的 postgres 数据库,每次尝试发出请求时,都会收到“证书链中的自签名证书”错误。 This is my Sequelize connection function:这是我的 Sequelize 连接功能:

const sequelize = new Sequelize({
  database: process.env.DB_NAME,
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  dialect: "postgres",
  dialectOptions: {
    ssl: true
  }
});

I searched for this problem, and I found 2 possible solutions: 1) include the certificate in the connection options, or 2) add NODE_TLS_REJECT_UNAUTHORIZED=0 as an env variable.我搜索了这个问题,并找到了 2 个可能的解决方案:1) 在连接选项中包含证书,或 2) 添加 NODE_TLS_REJECT_UNAUTHORIZED=0 作为环境变量。 Regarding the first solution, I have yet to find an example on how to do this using Sequelize and the documentation doesn't even mention this.关于第一个解决方案,我还没有找到关于如何使用 Sequelize 执行此操作的示例,文档甚至没有提到这一点。 With the 2nd solution it works just fine, but I understand that it shouldn't be used in a production environment as it disables Node SSL verification.使用第二个解决方案它工作得很好,但我知道它不应该在生产环境中使用,因为它禁用了节点 SSL 验证。

However, I'm also using Nginx as a reverse proxy and installed a LetsEncrypt SSL cert using certbot, which automatically configured my nginx server block to use SSL verification.但是,我也使用 Nginx 作为反向代理,并使用 certbot 安装了 LetsEncrypt SSL 证书,它自动将我的 nginx 服务器块配置为使用 SSL 验证。 This is my Nginx config:这是我的 Nginx 配置:

server {
  location / {
    proxy_pass http://localhost:3000;
    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;
  }
  listen [::]:443 ssl ipv6only=on; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/{mydomain}/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/{mydomain}/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

If I understand correctly, since nginx is already taking care of the SSL validation, I suppose it's okay to to do the 2nd solution since Nginx simply "redirects" requests to my Node app running on localhost:3000 via http after taking care of the SSL part.如果我理解正确,由于 nginx 已经在处理 SSL 验证,我想可以执行第二个解决方案,因为 Nginx 只是在处理 SSL 后通过 http“重定向”到我在 localhost:3000 上运行的 Node 应用程序的请求部分。 Am I right about this assumption?我对这个假设是否正确? If not, what is the correct way to configure Sequelize to include the cert and avoid the "Self signed certificate in certificate chain" error?如果没有,配置 Sequelize 以包含证书并避免“证书链中的自签名证书”错误的正确方法是什么?

I don't think this specific question was asked before, and I'm sorry if some of these questions seem "obvious", it's my first time doing this.我认为之前没有问过这个具体问题,如果其中一些问题看起来“显而易见”,我很抱歉,这是我第一次这样做。 Thanks for your help.谢谢你的帮助。

I have several servers in Node.js, and I never used them for https.我在 Node.js 中有几台服务器,但我从未将它们用于 https。 All ssl-related stuff I delegated to nginx, exactly like you did, though a bit simpler.我委托给 nginx 的所有 ssl 相关的东西,就像你做的那样,虽然有点简单。

Your guess is correct.你的猜测是正确的。 Nginx passes all requests to your server in plain text and wraps all responses into TLS records. Nginx 将所有请求以纯文本形式传递到您的服务器,并将所有响应包装到 TLS 记录中。 It works pretty fast, and certbot manages all my certificates.它运行得非常快,并且 certbot 管理我所有的证书。 What a relief ;)终于解脱了 ;)

My typical config looks like this:我的典型配置如下所示:

server {
  listen 443 ssl;
  listen [::]:443 ssl;
  server_name your.server.domain;

  location / {
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass                 http://localhost:3000/;
  }

  ssl_certificate /etc/letsencrypt/live/your.server.domain/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/your.server.domain/privkey.pem; # managed by Certbot
}

# Redirect from HTTP to HTTPS for all servers
server {
  listen 80;
  listen [::]:80;
  return 301 https://$host$request_uri;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM