【问题标题】:Configuring HTTPS for Express and Nginx为 Express 和 Nginx 配置 HTTPS
【发布时间】:2017-08-03 08:56:07
【问题描述】:

我正在尝试为我的 ExpressJS 应用程序配置 https 连接。 Express 服务器在 localhost:8080 和安全服务器 localhost:8443 运行。

这里是https相关的server.js代码:

var app = express();

var https = require('https');

const options = {
    cert: fs.readFileSync('/etc/letsencrypt/live/fire.mydomain.me/fullchain.pem'),
    key: fs.readFileSync('/etc/letsencrypt/live/fire.mydomain.me/privkey.pem')
};

app.listen(8080, console.log("Server running"));
https.createServer(options, app).listen(8443, console.log("Secure server running on port 8443"));

这是我的 Nginx 配置:

server {
    listen 80;
    listen [::]:80;
    server_name fire.mydomain.me;

    location / {
        proxy_pass http://localhost:8080;
        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;
    }
}

server {
    listen 443;
    listen [::]:443;
    server_name fire.mydomain.me;
    location / {
        proxy_pass https://localhost:8443;
        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;
    }
}

我做了什么:

  • 使用 Letsencrypt certonly 工具为域 fire.mydomain.me 生成 SSL 证书。
  • 配置 nginx。
  • 配置 server.js 节点应用程序。
  • 为 Ufw 中的 443 端口添加 TCP 规则。

我试过了

  • 注释 server.js 中的非 ssl 服务器行以强制连接通过 ssl 配置:当我尝试转到 fire.mydomain.me:443 但不转到“https: // fire.mydomain.me"。在这两种情况下,都没有 SSL。尝试访问 https://fire.mydomain.me 在 Google Chrome 中生成此消息“此网站不提供安全连接”。

  • 我首先按照本教程设置了我的 ssl 节点配置: https://medium.com/@yash.kulshrestha/using-lets-encrypt-with-express-e069c7abe625#.93jgjlgsc

【问题讨论】:

    标签: javascript node.js ssl nginx https


    【解决方案1】:

    您不需要在您的 nginx 反向代理和运行在同一主机上的 Node 应用程序之间使用 HTTPS。您可以将 HTTP 请求代理到端口 80 和将 HTTPS 请求代理到端口 443 到 Node 应用程序中的同一端口 - 在本例中为 8080 - 在这种情况下您不需要配置 TLS 证书。

    您可以将 server.js 文件更改为:

    var app = express();
    
    app.listen(8080, console.log("Server running"));
    

    并为端口 80 上的 HTTP 和端口 443 上的 HTTPS 使用具有 proxy_pass http://localhost:8080; 的 nginx 配置。

    这是通常的做法。加密环回接口上的流量不会增加任何安全性,因为要嗅探流量,您需要对盒子进行 root 访问,当您拥有它时,您可以读取证书并解密流量。考虑到https://nodejs.org/en/blog/vulnerability/ 上的大多数帖子都与 OpenSSL 相关这一事实,有人可能会争辩说,在 Node 中使用 SSL 会降低加密环回接口流量的特定情况下的安全性。有关详细信息,请参阅 GitHub 上 Node 项目上的 this discussion

    【讨论】:

    • 谢谢您,先生,您的解释非常有用,这很有效! :) `
    • 很好的解释!
    【解决方案2】:

    感谢@rsp 解决方案,这里是有效的 Nginx 配置:

    server {
    listen 80;
    listen 443 ssl;
    
    server_name fire.mydomain.me;
    
    ssl_certificate     /etc/letsencrypt/live/fire.mydomain.me/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/fire.mydomain.me/privkey.pem;
    
    location / {
        proxy_pass http://localhost:8080;
        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;
       }
    }
    

    【讨论】:

    • 这是否意味着数据在到达节点应用程序之前已经解密?如果我的 http 流量已经重定向到 https,即if ($scheme != "https"),我还需要所有 http_upgrade 标头内容吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2018-12-05
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多