首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过node-http-proxy启用HSTS?

如何通过node-http-proxy启用HSTS?
EN

Stack Overflow用户
提问于 2013-03-20 22:57:20
回答 1查看 4.8K关注 0票数 5

在node.js 0.8下,我在“路由表”模式下使用node-http-proxy,配置如下:

代码语言:javascript
复制
var httpProxy = require("http-proxy");
var config = require("./config");

proxyServer = httpProxy.createServer({
    hostnameOnly: true,
    router: {
        "one.example.com": "localhost:9000",
        "two.example.com": "localhost:9001"
    },
    https: {
        key: config.key,
        cert: config.cert,
        // mitigate BEAST: https://community.qualys.com/blogs/securitylabs/2011/10/17/mitigating-the-beast-attack-on-tls
        honorCipherOrder: true,
        ciphers: "ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH"
    }
})
proxyServer.listen(8000)

我想添加HSTS (超文本传输协议严格传输安全),以便兼容的浏览器将被告知始终使用SSL。为此,我需要让http-proxy添加头:

代码语言:javascript
复制
Strict-Transport-Security: max-age=60000

(或其他最大年龄)。如何才能让node-http-proxy高效地追加这个头部?

EN

回答 1

Stack Overflow用户

发布于 2015-03-27 12:54:45

对于你的例子,我不确定,因为这个老问题似乎使用了http-proxy@0.8。然而,下面是我对http-proxy@1.0.0所做的工作

代码语言:javascript
复制
var httpProxy = require('http-proxy');

// https server to decrypt TLS traffic and direct to a normal HTTP backend
var proxy = httpProxy.createProxyServer({
  target: {
    host: 'localhost',
    port: 9009 // or whatever port your local http proxy listens on
  },
  ssl: {
    key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
    cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
  }
}).listen(443); // HTTPS listener for the real server

// http server that redirects all requests to their corresponding
// https urls, and allows standards-compliant HTTP clients to 
// prevent future insecure requests.
var server = http.createServer(function(req, res) {
  res.statusCode = 301;
  res.setHeader('Location', 'https://' + req.headers.host.split(':')[0] + req.url);
  res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
  return res.end();
});

server.listen(80); // HTTP listener for the old HTTP clients
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15527099

复制
相关文章

相似问题

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