1

根据标题,我有一个 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”标头。

4

3 回答 3

6

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

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);
}
于 2013-02-28T15:27:43.570 回答
4

遇到完全相同的问题,但以不同的方式解决了它。如果您使用 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();
});
于 2013-08-27T00:52:22.800 回答
0

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

于 2021-07-24T14:23:19.057 回答