首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJs头盔hsts配置

NodeJs头盔hsts配置
EN

Stack Overflow用户
提问于 2019-12-18 00:51:11
回答 1查看 328关注 0票数 0

我对NodeJs不是很熟悉,但有人要求我支持NodeJs应用程序,以发现安全漏洞。我在NodeJs上使用Express web服务器。要求是使用hsts。所以我安装了头盔模块,并添加了以下代码:

app.use(helmet.hsts());

我相信hsts模块将强制客户端通过https进行连接,而不支持通过http进行连接。

问:这是否意味着现在必须为NodeJs express服务器提供SSL证书。

队列

EN

回答 1

Stack Overflow用户

发布于 2020-04-09 15:27:18

可以这样做,但这不是必需的。如果您想要像导入https那样创建服务器,而不是从http创建服务器

代码语言:javascript
复制
const app = require('express')();
const https = require('https');
const fs = require('fs');

// ...

https.createServer({
    key: fs.readFileSync('./key.pem'),
    cert: fs.readFileSync('./cert.pem'),
    passphrase: 'YOUR PASSPHRASE'
}, app)
.listen(443);

另一个流行的解决方案是仍然使用普通的超文本传输协议运行你的应用程序,然后在它前面使用一个代理(例如NGINX,HAProxy或Apache)来做SSL并建立到客户端的连接。

例如,这是一个NGINX的例子(应用程序在http://localhost:3000上运行)

代码语言:javascript
复制
server {

    listen 443;
    server_name yourhost.domain.com;

    ssl_certificate           /etc/nginx/cert.crt;
    ssl_certificate_key       /etc/nginx/cert.key;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;

      # Fix the “It appears that your reverse proxy set up is broken" error.
      proxy_pass          http://localhost:3000;
      proxy_read_timeout  90;

      proxy_redirect      http://localhost:3000 https://yourhost.domain.com;
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59378734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档