简体   繁体   English

Sails.JS HTTP + HTTPS

[英]Sails.JS HTTP + HTTPS

I am trying to figure out how to lift a sails app that responds to both HTTP and HTTPS requests. 我试图找出如何解除响应HTTP和HTTPS请求的风帆应用程序。 I used the config/local.js method of configuring express like so (detailed here ): 我使用config / local.js配置express这样的方法(详见这里 ):

 var fs = require('fs');

module.exports = {

  port: process.env.PORT || 1337,
  environment: process.env.NODE_ENV || 'development',

  express: { serverOptions : {
    key: fs.readFileSync('ssl/key.pem'),
    cert: fs.readFileSync('ssl/cert.pem')
  }}

};

However, while this works, it results in the server only being able to serve HTTPS requests. 但是,虽然这有效,但它会导致服务器只能提供HTTPS请求。 Has anyone figured out how to get this done, without creating a separate HTTP only server that redirects to port 443 ? 有没有人想出如何完成这项工作,而没有创建一个单独的HTTP服务器重定向到端口443?

Thanks 谢谢

1-Uncomment the path to your SSL Certificates in your local.js or add path to your SSL Certificates in your config/env/production.js. 1 - 取消注释local.js中SSL证书的路径,或在config / env / production.js中添加SSL证书的路径。

    module.exports  = {
 ssl: {
     ca: require('fs').readFileSync(__dirname + '/ssl/ca.crt'),
     key: require('fs').readFileSync(__dirname + '/ssl/key.key'),
     cert: require('fs').readFileSync(__dirname + '/ssl/cert.crt')
   },
 port: 443
}

2-Add a policies section in your config/env/production.js 2 - 在config / env / production.js中添加策略部分

    module.exports  = {
 ssl: {
     ca: require('fs').readFileSync(__dirname + '/ssl/ca.crt'),
     key: require('fs').readFileSync(__dirname + '/ssl/key.key'),
     cert: require('fs').readFileSync(__dirname + '/ssl/cert.crt')
   },
 port: 443,
 policies: {
    '*': 'isHTTPS'
   }
}

3-Create a isHTTPS.js policy in your api/policies folder. 3 - 在api / policies文件夹中创建isHTTPS.js策略。 This policy will redirect the HTTP request to HTTPS 此策略将HTTP请求重定向到HTTPS

  module.exports = function(req, res, next) {
    if (req.secure) {
        // Already https; don't do anything special.
        next();
    } else {
        // Redirect to https.
        res.redirect('https://' + req.headers.host + req.url);
    }
};

4-Then we will edit the config/bootstrap.js file and listen to port 80 if the environment is production, so that we can redirect the requests to 443 ie SSL 4 - 然后我们将编辑config / bootstrap.js文件并在环境生产时监听端口80,以便我们可以将请求重定向到443,即SSL

    var http = require( 'http' );
module.exports.bootstrap = function(cb) {
    //If the environment is production, then listen on port 80 
    if(sails.config.environment === "production") {
        http.createServer( sails.hooks.http.app ).listen( 80 );        
    }
    cb();
}

I was able to get http and https running with sails by modifying my configs/local.js file: 通过修改我的configs / local.js文件,我能够通过sails运行http和https:

var fs = require('fs');
var local = ... // Your default local.js settings should go here
if(require('optimist').argv.https) {
    local.express = {
        serverOptions : {
            key: fs.readFileSync('ssl/server.key'),
            cert: fs.readFileSync('ssl/server.crt')
        }
    };
    local.port = 1338; // This port should be different than your default port
}
module.exports = local;

Now you will need to run your sails app twice. 现在,您需要运行两次风帆应用程序。 The first time type in your regular sails lift command. 第一次输入常规sails lift指令。 The second time run sails lift --https 第二次跑sails lift --https

This should allow you to have both http and https servers running off of the same codebase. 这应该允许您使用相同的代码库运行http和https服务器。 As some other people have mentioned, nginx is a much better solution for handling this sort of this, but if you are doing local development and don't want to have to install nginx the solution above is just fine. 正如其他人提到的那样,nginx是处理这种情况的更好的解决方案,但是如果你正在进行本地开发并且不想安装nginx,那么上面的解决方案就可以了。

To have both HTTP & HTTPS, avoiding the redirection way: 同时拥有HTTP和HTTPS,避免重定向方式:

Configure your SSL key & cert and HTTPS port in your /config/env/production.js : /config/env/production.js中配置SSL密钥和证书以及HTTPS端口

var fs = require( 'fs' );
module.exports = {
    port: 443,
    ssl: {
        key: fs.readFileSync( 'ssl_key.key' ),
        cert: fs.readFileSync( 'ssl_key.crt' )
    }
    //, ...
};

Then, listen port 80 with a second HTTP server into /config/bootstrap.js : 然后,使用第二个HTTP服务器将端口80监听/config/bootstrap.js

var http = require( 'http' );
module.exports.bootstrap = function ( cb ) {
    // ...
    if ( process.env.NODE_ENV == 'production' )
        http.createServer( sails.hooks.http.app ).listen( 80 );
    // ...
};

Check this out: https://github.com/balderdashy/sails/issues/862 看看这个: https//github.com/balderdashy/sails/issues/862

module.exports.bootstrap = function (cb) {
    var express = require("express"),
         app = express();

    app.get('*', function(req,res) {  
        res.redirect('https://' + req.headers.host + req.url)
    }).listen(80);

    cb();
};

If you are using sails 0.10.x you may need to move this to module.exports.express (config/express.js) I'm not sure... 如果您使用的是风帆0.10.x,您可能需要将其移至module.exports.express(config / express.js)我不确定...

As an update from 0.9 to 0.10 , the local.js file should now have 作为从0.9到0.10的更新,local.js文件现在应该具有

ssl : {
key: fs.readFileSync(‘server.key’),
cert: fs.readFileSync(‘server.crt’)
}

instead of 代替

express : {
        serverOptions : {
            key: fs.readFileSync('ssl/server.key'),
            cert: fs.readFileSync('ssl/server.crt')
        }
    };

I've been wondering for running sailsjs on HTTPS and after going back and forth here is the solution working for me. 我一直想知道在HTTPS上运行sailsjs,在这里来回运行是我的解决方案。

  1. I copied .crt and .key file in project directory under ssl folder 我在.sl文件夹下的项目目录中复制了.crt和.key文件
  2. config/local.js updated with following as I'm using sailsjs 0.12.3 config / local.js随后更新,因为我正在使用sailsjs 0.12.3

    \nssl: { key: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mykey.key')), cert: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mycert.crt')) }\nssl: { key: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mykey.key')), cert: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mycert.crt')) } 

目前看起来没有办法在帆中进行此操作,因此我最终只在服务器上使用了nginx并为HTTP-> HTTPS创建了重定向规则。

You must take care of SSL & HTTPS , If both use same port by proxy. 您必须处理SSLHTTPS ,如果两者都使用相同的代理端口。

var express = require("express"),
     app = express();

app.get('*', function(req,res) {  

    if(req.isSocket){           
        return res.redirect('wss://' + req.headers.host + req.url)  
    }
    else{
        return res.redirect('https://' + req.headers.host + req.url)    
    }

}).listen(80);

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

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