如何使用自定义域强制执行到 Google App Engine 的 HTTPS 流量?
How to enforce HTTPS traffic to Google App Engine with custom domain?
我在 Google 域 (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.com and https://www.example.com。但我也可以访问这些域的不安全 http 版本。
如何将我的 Google 域设置为始终使用 https?如果有人输入 http://example.com,我希望它转到 https 站点。
记录:
我的裸域(example.com)有4条A记录和4条AAAA记录
我的 www.example.com 域有 1 条别名为 www 的 CNAME 记录。
您是否尝试过在 app.yaml
的处理程序中设置 secure: always
?
handlers:
- url: /youraccount/.*
script: accounts.app
login: required
secure: always
always
Requests for a URL that match this handler that do not use
HTTPS are automatically redirected to the HTTPS URL with the same
path. Query parameters are preserved for the redirect
https://cloud.google.com/appengine/docs/standard/python/config/appref#handlers_element
secure: always
仍然适用于所有标准环境,但安全选项在所有 灵活环境 中已被 弃用 ,see documentation here or here for Node.js.
如果您在当前环境中需要此功能,建议的解决方案需要更改您的应用程序代码。使用自定义 HTTP header X-Forwarded-Proto
将 HTTP 流量重定向到 HTTPS,或者使用 HTTP Strict Transport Security response header.
不确定您使用的是哪种后端语言,但您可以通过检查请求 header 然后重定向来 brute-force 到 ssl。示例:
if request.environ.get('HTTPS') == 'off':
return redirect('https://www.example.com' + request.environ.get('PATH_INFO'), 301)
Alex 的回答(见评论)让我走上了正确的道路。
先meteor add gadicohen:headers
添加headers
信息。
在我的路由逻辑(Iron Router on Meteor)中的一个 before hook 中,我检查 x-forwarded-proto
是否为 http
。如果是这样,请将 http
替换为 https
并转至 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')
}
}
});
对于 Java 和 Spring 使用默认 Tomcat 启动,以下设置适用于 Google App Engine flex:
server.tomcat.remoteip.remote-ip-header=x-forwarded-for
server.tomcat.remoteip.protocol-header=x-forwarded-proto
我在 Google 域 (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.com and https://www.example.com。但我也可以访问这些域的不安全 http 版本。
如何将我的 Google 域设置为始终使用 https?如果有人输入 http://example.com,我希望它转到 https 站点。
记录: 我的裸域(example.com)有4条A记录和4条AAAA记录
我的 www.example.com 域有 1 条别名为 www 的 CNAME 记录。
您是否尝试过在 app.yaml
的处理程序中设置 secure: always
?
handlers:
- url: /youraccount/.*
script: accounts.app
login: required
secure: always
always
Requests for a URL that match this handler that do not use HTTPS are automatically redirected to the HTTPS URL with the same path. Query parameters are preserved for the redirect
https://cloud.google.com/appengine/docs/standard/python/config/appref#handlers_element
secure: always
仍然适用于所有标准环境,但安全选项在所有 灵活环境 中已被 弃用 ,see documentation here or here for Node.js.
如果您在当前环境中需要此功能,建议的解决方案需要更改您的应用程序代码。使用自定义 HTTP header X-Forwarded-Proto
将 HTTP 流量重定向到 HTTPS,或者使用 HTTP Strict Transport Security response header.
不确定您使用的是哪种后端语言,但您可以通过检查请求 header 然后重定向来 brute-force 到 ssl。示例:
if request.environ.get('HTTPS') == 'off':
return redirect('https://www.example.com' + request.environ.get('PATH_INFO'), 301)
Alex 的回答(见评论)让我走上了正确的道路。
先meteor add gadicohen:headers
添加headers
信息。
在我的路由逻辑(Iron Router on Meteor)中的一个 before hook 中,我检查 x-forwarded-proto
是否为 http
。如果是这样,请将 http
替换为 https
并转至 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')
}
}
});
对于 Java 和 Spring 使用默认 Tomcat 启动,以下设置适用于 Google App Engine flex:
server.tomcat.remoteip.remote-ip-header=x-forwarded-for
server.tomcat.remoteip.protocol-header=x-forwarded-proto