1
votes

I have my node sails project in Google App Engine in need to redirect http to https.

With google Docs, I need to add handlers with secure: always in app.yaml file to achieve secure redirection, But its not working for me.

My app.yaml

env: flex
runtime: nodejs
manual_scaling:
  instances: 1
resources:
  cpu: 2
  memory_gb: 8
  disk_size_gb: 200
handlers:
- url: /.*
  script: auto
  secure: always
  redirect_http_response_code: 301
env_variables:
  SQL_PASSWORD: "------"
  SQL_DATABASE: "-----"
  INSTANCE_CONNECTION_NAME: "-----"

Am I missing anything.

2
What it doesn't work ? The deployment or the http redirection to HTTPS? Now you can reach you website in HTTP and HTTPS ?guillaume blaquiere

2 Answers

1
votes

App Engine Flex does not support the option secure: always

That option is for App Engine Standard.

You will need to perform HTTP to HTTPS redirection in your webserver code.

Here is an example:

app.use(function(request, response){
  if(!request.secure){
    response.redirect("https://" + request.headers.host + request.url);
  }
});
1
votes

App Engine Flex does not support the option secure: always for App Engine Standard it supports.

With Sails created policies for redirection with reference John Hanley Answer

config/env/production.js

module.exports = {
   ...
   ......
   .........
   policies:{
    '*': 'isHTTPS'
   }
}

api/policies/isHTTPS.js

module.exports = function(req, res, next) {
  var schema = req.headers['x-forwarded-proto'] || '';

  if (schema === 'https') {
      // if its a request from myweb.backend.appspot.com
      if (req.headers.host !== 'myweb.com') {
        res.redirect('https://' + 'myweb.com' + req.url);
      } else {
        next();
      }
  } else {
      // Redirect to https.
      res.redirect('https://' + ((req.headers.host !== 'myweb.com') ? 'myweb.com' : req.headers.host)  + req.url);
  }
};