javascript – 为Express和Nginx配置HTTPS

前端之家收集整理的这篇文章主要介绍了javascript – 为Express和Nginx配置HTTPS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

原文链接:https://www.f2er.com/nginx/434407.html

猜你在找的Nginx相关文章