HTTPS 重定向不适用于 Google App Engine 与 app.yaml

Https redirection not working with Google App Engine with app.yaml

我在 Google App Engine 中有我的 node sails 项目,需要将 http 重定向到 https。

使用 google Docs,我需要在 app.yaml 文件中添加带有 secure: always 的处理程序实现安全重定向,但它对我不起作用。

我的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: "-----"

我是不是漏了什么。

A​​pp Engine Flex 不支持选项 secure: always

该选项适用于 App Engine 标准。

您需要在网络服务器代码中执行 HTTP 到 HTTPS 的重定向。

这是一个例子:

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

A​​pp Engine Flex 不支持选项 secure: always for App Engine Standard 它支持。

使用 Sails 创建重定向策略并参考 John Hanley 的回答

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);
  }
};