简体   繁体   English

在Openshift上使用https创建Express JS 4.0应用程序,包括http重定向

[英]Creating an Express JS 4.0 application with https on Openshift, including http redirection

Following another SO question , the latest I have been trying is (see ligatures.net ): 在另一个SO问题之后 ,我一直在尝试的最新问题是(参见ligatures.net ):

self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
self.port      = process.env.OPENSHIFT_NODEJS_PORT || 443;

if (typeof self.ipaddress === "undefined") {
    self.ipaddress = "127.0.0.1";
};

...
self.app = express();  // 4.8.7

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

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

    if ( !req.secure() ) {
        var tmp = 'https://' + process.env["DOMAIN_NAME"] + req.originalUrl;
        console.log("Redirect to: " + tmp);
        res.redirect(tmp);
    } else {
        next();
    }               

});

...

// Creating a http server
https.createServer(self.app).listen(self.port, self.ipaddress,
    function(err) {
        if (err) {
            console.log("Node server error: " + err.toString());
        } else {
            console.log('%s: Node server started on %s:%d ...',
                Date(Date.now() ), self.ipaddress, self.port);
        }
});

In the Openshift logs, I get: 在Openshift日志中,我得到:

Property 'secure' of object # is not a function 对象#的属性“安全”不是函数

This is the line: if ( !req.secure() ) { 这是一行: if ( !req.secure() ) {

The certificates are loaded in the console. 证书将加载到控制台中。 The application starts successfully on port 8080. 应用程序在端口8080上成功启动。

Why am I getting this message and how should I create a secured Express 4.0 https application in OpenShift? 为什么我收到此消息,如何在OpenShift中创建安全的Express 4.0 https应用程序? Does anyone have operational code to share? 有人有共享的操作代码吗? Thanks! 谢谢!

UPDATE UPDATE

I have updated the redirection as following: 我更新了重定向,如下所示:

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

    if ( req.headers['x-forwarded-proto'] === 'http' ) { 

        var tmp = 'https://' + req.headers.host + req.originalUrl;
        console.log("SERVER redirect to: " + tmp);
        res.redirect(tmp);

    } else {

        var pt = req.protocol || "";
        var ho = req.headers.host || "";
        var ur = req.originalUrl || "";

        console.log("\nProtocol: " + pt
                  + "\nHost   : " + ho
                  + "\nUrl    : " + ur);

        var tmp = req.protocol + '://' + req.headers.host + req.originalUrl;
        console.log("SERVER no redirect: " + tmp);
        next();

    }

I see a couple of the following from the console: 我在控制台中看到以下几种情况:

SERVER no redirect: http://undefined/
Protocol: http
Host   :
Url    : /

and my application still does not work. 而我的申请仍然无效。 It looks like a bug to me. 对我来说这看起来像个错误。

I have opened an issue: https://bugzilla.redhat.com/show_bug.cgi?id=1138137 我已经打开了一个问题: https//bugzilla.redhat.com/show_bug.cgi?id = 1138137

I am also behind Cloudflare, which may be part of the issue. 我也支持Cloudflare,这可能是问题的一部分。

There were two issues: 有两个问题:

i) If you are on Cloudflare's free plan, then it won't forward the SSL requests. i)如果您使用的是Cloudflare的免费计划,那么它将不会转发SSL请求。 If you disactivate Cloudflare (while keeping their DNS for example), then the SSL requests are forwarded to Openshift. 如果您停用Cloudflare(例如保留其DNS),则SSL请求将转发到Openshift。

ii) For some unknown reason, some empty requests reach my application on Openshift when starting it, and when browsing pages later too: ii)由于某些未知原因,一些空的请求在启动时到达我在Openshift上的应用程序,以及稍后浏览页面时:

req.path: /
req.protocol: http
req.originalUrl: /
req.secure: false

The code mentioned in Openshift's KB article cannot work well as is, for two reasons. 由于两个原因,Openshift的KB文章中提到的代码不能正常工作。 First, req.path does not take query parameters into account. 首先, req.path不考虑查询参数。 One should use req.originalUrl instead in Express 4.0. 应该在Express 4.0中使用req.originalUrl

Second instead of using self.app.get(r, redirectSec, self.routes[r]); 第二,而不是使用self.app.get(r, redirectSec, self.routes[r]); , one should add the middleware with self.app.use in Express 4.0: ,应该在Express 4.0中使用self.app.use添加中间件:

My HTTP to HTTPS forwarding code is now: 我的HTTP到HTTPS转发代码现在是:

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

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

        if ( req.headers['x-forwarded-proto'] === 'http' ) { 

            var tmp = 'https://' + req.headers.host + req.originalUrl;
            res.redirect(tmp);

        } else {

            return next();

        }

    }

});

All seems to work fine now. 现在一切似乎都很好。

req.secure是一个属性,而不是一个函数。

Try this bit of code from the help.openshift.com website: 试试help.openshift.com网站上的这段代码:

function redirectSec(req, res, next) {
        if (req.headers['x-forwarded-proto'] == 'http') { 
            res.redirect('https://' + req.headers.host + req.path);
        } else {
            return next();
        }
    }

Which can be found in this KB article: https://help.openshift.com/hc/en-us/articles/202398810-How-to-redirect-traffic-to-HTTPS- 可以在这篇知识库文章中找到: https//help.openshift.com/hc/en-us/articles/202398810-How-to-redirect-traffic-to-HTTPS-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM