【问题标题】:How to detect HTTPS redirection on Azure Websites?如何检测 Azure 网站上的 HTTPS 重定向?
【发布时间】:2013-02-26 08:29:50
【问题描述】:

根据标题,我有一个 Node.js 应用程序,我希望能够检测请求是通过 HTTPS 还是通过 HTTP 发出的。到目前为止,我的重定向看起来像这样:

// Ensure the page is secure, or that we are running a development build
if (req.headers['x-forwarded-proto'] === 'https' || process.env.NODE_ENV === 'development') {
    res.render('index');
} else {
    winston.info('Request for login page made over HTTP, redirecting to HTTPS');
    res.redirect('https://' + req.host);
}

这在 Nodejitsu 上运行良好,但重定向的 HTTPS 请求没有在 Azure 上设置“x-forwarded-proto”标头。

【问题讨论】:

  • 看来我可以使用“x-arr-ssl”标头了。
  • 据我了解,标头“x-forwarded-proto”来自一些实际的客户端。虽然重定向一些正在运行的间歇性应用程序,但这些重定向不包括 x-forwarded-proto 标头。我觉得使用其他一些参数来识别协议而不是标头信息会更好。
  • 您建议使用什么? Azure 和 Nodejitsu 都使用代理将 HTTPS 流量重定向到 HTTP 服务器。那里没有其他我可以使用的东西吗?
  • 您是否检查过代理中是否发生了任何自动重定向?
  • 是的,我知道它会重定向。

标签: node.js azure


【解决方案1】:

我认为我的评论是正确的:

X-ARR-SSL 似乎是要检查的标头。

// Ensure the page is secure, or that we are running a development build
if (req.headers['x-forwarded-proto'] === 'https' || req.headers['x-arr-ssl'] || process.env.NODE_ENV === 'development') {
    res.render('index');
} else {
    winston.info('Request for login page made over HTTP, redirecting to HTTPS');
    res.redirect('https://' + req.host);
}

【讨论】:

    【解决方案2】:

    遇到了完全相同的问题,但以不同的方式解决了它。如果您使用 Express 并启用信任代理,则 req.protocol 将获取 x-forwarded-proto 标头。 Azure 不设置 x-forwarded-proto 标头,但您可以使用 x-arr-ssl 标头手动破解它,以便 req.protocol 将在您的应用程序的其余部分返回正确的值。

    这里有一个要点:https://gist.github.com/freshlogic/6348417

    var express = require('express');
    
    var app = express();
    app.enable('trust proxy');
    
    // HACK: Azure doesn't support X-Forwarded-Proto so we add it manually
    app.use(function(req, res, next) {
        if(req.headers['x-arr-ssl'] && !req.headers['x-forwarded-proto']) {
            req.headers['x-forwarded-proto'] = 'https';
        }
    
        return next();
    });
    

    【讨论】:

      【解决方案3】:

      2021 年更新: 这是一个非常古老的答案。长期以来,所有支持自定义域的应用服务计划都有一个选项。转到应用服务的 Azure 门户中的自定义域边栏选项卡,然后设置仅 HTTPS 复选框。这甚至会在流量到达应用服务之前重定向。

      【讨论】:

        猜你喜欢
        • 2020-08-12
        • 2022-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        • 2017-01-22
        • 2017-09-21
        相关资源
        最近更新 更多