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

时间:2022-02-23 22:45:00

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

对象#的属性'secure'不是函数

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? Does anyone have operational code to share? Thanks!

为什么我收到此消息,如何在OpenShift中创建安全的Express 4.0 https应用程序?有没有人有操作代码可以共享?谢谢!

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,这可能是问题的一部分。

3 个解决方案

#1


4  

There were two issues:

有两个问题:

i) If you are on Cloudflare's free plan, then it won't forward the SSL requests. If you disactivate Cloudflare (while keeping their DNS for example), then the SSL requests are forwarded to Openshift.

i)如果您使用的是Cloudflare的免费计划,那么它将不会转发SSL请求。如果您停用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. First, req.path does not take query parameters into account. One should use req.originalUrl instead in Express 4.0.

由于两个原因,Openshift的KB文章中提到的代码不能正常工作。首先,req.path不考虑查询参数。应该在Express 4.0中使用req.originalUrl。

Second instead of using self.app.get(r, redirectSec, self.routes[r]);, one should add the middleware with self.app.use in Express 4.0:

其次,而不是使用self.app.get(r,redirectSec,self.routes [r]);,应该在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.

现在一切似乎都很好。

#2


3  

req.secure is a property, not a function.

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

#3


2  

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-

#1


4  

There were two issues:

有两个问题:

i) If you are on Cloudflare's free plan, then it won't forward the SSL requests. If you disactivate Cloudflare (while keeping their DNS for example), then the SSL requests are forwarded to Openshift.

i)如果您使用的是Cloudflare的免费计划,那么它将不会转发SSL请求。如果您停用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. First, req.path does not take query parameters into account. One should use req.originalUrl instead in Express 4.0.

由于两个原因,Openshift的KB文章中提到的代码不能正常工作。首先,req.path不考虑查询参数。应该在Express 4.0中使用req.originalUrl。

Second instead of using self.app.get(r, redirectSec, self.routes[r]);, one should add the middleware with self.app.use in Express 4.0:

其次,而不是使用self.app.get(r,redirectSec,self.routes [r]);,应该在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.

现在一切似乎都很好。

#2


3  

req.secure is a property, not a function.

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

#3


2  

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-