加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

javascript – 为Express和Nginx配置HTTPS

发布时间:2020-12-13 21:35:57 所属栏目:Nginx 来源:网络整理
导读:我正在尝试配置我的ExpressJS应用程序以进行https连接. Express服务器运行在localhost:8080和安全的localhost:8443. 这是与https相关的server.js代码: var app = express();var https = require('https');const options = { cert: fs.readFileSync('/etc/

我正在尝试配置我的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;
    }
}

我做了什么 :

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

我试过了

>在server.js中注释not-ssl服务器行以强制连接通过ssl配置:当我尝试访问fire.mydomain.me:443而不是“https:// fire.mydomain”时,这会为页面提供服务.我”.在这两种情况下,都没有SSL.尝试访问https:// fire.mydomain.me会在Google Chrome中生成此消息“此网站无法提供安全连接”.
>我首先按照本教程设置我的ssl节点配置:
https://medium.com/@yash.kulshrestha/using-lets-encrypt-with-express-e069c7abe625#.93jgjlgsc

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

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

var app = express();

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读