我有一个简单的容器化网络应用程序(Nginx提供HTML和Javascript),我部署到Google Cloud Run。
问题是,即使我已经验证并更新了DNS记录,我也无法强制使用HTTPS连接。如果用户仍然可以访问我的Cloud Run应用程序的不安全的http端点。
如何设置强制或重定向用户使用HTTPS的Google Cloud Run服务?
LB发送一个名为X-Forwarded-Proto
的标题,其中包含http
或https
,因此您可以轻松地使用301 Moved Permanently
重定向,以防您检测到。
使用Nginx编辑问题的示例:http://scottwb.com/blog/2013/10/28/always-on-https-with-nginx-behind-an-elb/
示例转码:
func main() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if request.Header["X-Forwarded-Proto"][0] == "http" {
http.Redirect(writer, request, "https://" + request.Host + request.RequestURI, http.StatusMovedPermanently)
return
}
fmt.Printf("Request: %+v, headers: %+v \n", request, request.Header)
writer.Write([]byte("hello world"))
})
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}