【问题标题】:Node application: Http to Https redirection does not work for localhost节点应用程序:Http 到 Https 的重定向不适用于 localhost
【发布时间】:2014-09-02 13:35:01
【问题描述】:

我将 HTTPS 添加到 node.js 应用程序,同时将 HTTP 转发到 HTTPS。我正在使用以下代码:

    // Trusting proxy
    self.app.enable('trust proxy');

    // Http -> Https redirection middleware
    self.app.use(function (req, res, next) {

        var schema = req.protocol.toLowerCase();
        console.log("Redirection check, schema: " + schema);

        if (schema === 'https') {
            console.log("Next");
            next();
        } else {
            var tmp = 'https://' + req.headers.host + req.url;
            console.log("Redirect to: " + tmp);
            res.redirect(tmp);
        }

    });

当我浏览 https://localhost:8443/index.html 时一切正常,页面打开正常。

但是当我尝试http://localhost:8443/index.html 时,Chrome 似乎将 url 重定向或重写为localhost:8443/index.html,并且 Chrome 永远等待 localhost。我的应用程序没有控制台消息。我在 Windows 7 下使用 Chrome。

我的代码有什么问题?

【问题讨论】:

    标签: javascript node.js redirect https


    【解决方案1】:

    Chrome 只是隐藏了 URL 的 http:// 部分,这是正常的。

    您不能在同一端口上运行 httphttps

    您在地址中指定了http,因此浏览器会尝试使用 HTTP 连接。服务器正在侦听 HTTPS,所以它失败了。

    【讨论】:

    • 有道理。我忘记了,在 Openshift 下,你只需要监听一个端口。但是,当然,我需要在笔记本电脑上设置第二台服务器。现在就试试吧。
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2014-04-06
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多