8

我在 Google Domains (www.example.com) 上有一个网站,它由 Gcloud 托管。我按照此处列出的说明设置 SSL 和 https:https: //cloud.google.com/appengine/docs/standard/python/securing-custom-domains-with-ssl

基本上,我只是跑了gcloud beta app domain-mappings update example.com --certificate-management='AUTOMATIC'

现在我确实可以访问https://example.comhttps://www.example.com了。但我也可以访问这些域的不安全 http 版本。

如何将我的 Google 域设置为始终使用 https?如果有人键入http://example.com,我希望它转到 https 站点。

记录:我的裸域(example.com)有 4 条 A 记录和 4 条 AAAA 记录。

我的 www.example.com 域有 1 条别名为 www 的 CNAME 记录。

4

5 回答 5

4

您是否尝试过secure: always在您的处理程序中设置app.yaml

handlers:
- url: /youraccount/.*
  script: accounts.app
  login: required
  secure: always

总是

与此处理程序匹配但不使用 HTTPS 的 URL 请求会自动重定向到具有相同路径的 HTTPS URL。为重定向保留查询参数

https://cloud.google.com/appengine/docs/standard/python/config/appref#handlers_element

于 2018-02-12T18:46:42.263 回答
3

secure: always在所有标准环境中仍然有效,但在所有灵活环境中已弃用安全选项,请参阅此处此处的 Node.js文档。

如果您在当前环境中需要此功能,建议的解决方案需要更改您的应用程序代码。使用自定义 HTTP 标头X-Forwarded-Proto将 HTTP 流量重定向到 HTTPS,或使用HTTP Strict Transport Security 响应标头

于 2018-02-12T21:27:41.797 回答
2

不确定您使用的是哪种后端语言,但您可以通过检查请求标头然后重定向来强制使用 ssl。例子:

if request.environ.get('HTTPS') == 'off':
    return redirect('https://www.example.com' + request.environ.get('PATH_INFO'), 301)
于 2018-02-12T21:28:28.253 回答
1

对于使用默认 Tomcat 的 Java 和 Spring Boot,以下设置适用于 Google App Engine flex:

server.tomcat.remoteip.remote-ip-header=x-forwarded-for
server.tomcat.remoteip.protocol-header=x-forwarded-proto
于 2020-11-16T19:17:33.207 回答
1

亚历克斯的回答(见评论)让我走上了正确的道路。

首先meteor add gadicohen:headers添加headers信息。

在我的路由器逻辑(Meteor 上的 Iron Router)中,我检查了是否x-forwarded-protohttp. 如果是这样,请替换httphttps并转到该 URL。我确保我不在本地主机上,这样我就可以开发网站

Router.onBeforeAction(function () {
    <some other logic>
    // Redirect http to https
    if (headers.get('x-forwarded-host') !== "localhost:3000") {
        if (headers.get('x-forwarded-proto') === "http") {
            window.location = window.location.href.replace('http', 'https')
        }
    }
});
于 2018-02-12T23:23:04.700 回答