【问题标题】:Https redirection not working with Google App Engine with app.yamlHttps 重定向不适用于带有 app.yaml 的 Google App Engine
【发布时间】:2020-02-05 23:51:32
【问题描述】:

我在 Google App Engine 中有我的 nodesails 项目需要将 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: "-----"

我有什么遗漏吗?

【问题讨论】:

  • 什么不起作用?部署或 http 重定向到 HTTPS?现在您可以通过 HTTP 和 HTTPS 访问您的网站了吗?

标签: google-app-engine https google-cloud-platform http-redirect app.yaml


【解决方案1】:

App Engine Flex 不支持选项secure: always

该选项适用于 App Engine 标准。

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

这是一个例子:

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

【讨论】:

  • 我遇到了同样的问题。但是你能分享一下描述那个的 URL 吗?
  • @scipilot 精确的解决方案取决于语言、运行时等。Google 前端 (GFE) 包含指示客户端如何连接到 GFE 的标头。我提供了一个例子。答案是You will need to perform HTTP to HTTPS redirection in your webserver code.
【解决方案2】:

App Engine Flex 不支持安全选项:始终适用于它支持的 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);
  }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2022-01-24
    相关资源
    最近更新 更多