Sails.JS HTTP + HTTPS

后端 未结 8 2214
夕颜
夕颜 2021-01-02 04:49

I am trying to figure out how to lift a sails app that responds to both HTTP and HTTPS requests. I used the config/local.js method of configuring express like so (detailed h

相关标签:
8条回答
  • 2021-01-02 05:27

    Doesnt look like there is a way to do this within sails at the moment so I ultimately just used nginx on the server and created a redirect rule for HTTP->HTTPS.

    0 讨论(0)
  • 2021-01-02 05:35

    You must take care of SSL & HTTPS , If both use same port by proxy.

    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);
    
    0 讨论(0)
  • 2021-01-02 05:36

    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.

        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

        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. This policy will redirect the HTTP request to 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 i.e 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();
    }
    
    0 讨论(0)
  • 2021-01-02 05:40

    Check this out: 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 讨论(0)
  • 2021-01-02 05:40

    I've been wondering for running sailsjs on HTTPS and after going back and forth here is the solution working for me.

    1. I copied .crt and .key file in project directory under ssl folder
    2. config/local.js updated with following as I'm using sailsjs 0.12.3

      
      ssl: {
        key: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mykey.key')),
        cert: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mycert.crt'))
      }
      
      
      
    0 讨论(0)
  • 2021-01-02 05:42

    As an update from 0.9 to 0.10 , the local.js file should now have

    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')
            }
        };
    
    0 讨论(0)
提交回复
热议问题