如何在Google Cloud Run中将所有http流量重定向到https

问题描述 投票:2回答:1

我有一个简单的容器化网络应用程序(Nginx提供HTML和Javascript),我部署到Google Cloud Run。

问题是,即使我已经验证并更新了DNS记录,我也无法强制使用HTTPS连接。如果用户仍然可以访问我的Cloud Run应用程序的不安全的http端点。

如何设置强制或重定向用户使用HTTPS的Google Cloud Run服务?

google-cloud-platform google-cloud-run
1个回答
5
投票

LB发送一个名为X-Forwarded-Proto的标题,其中包含httphttps,因此您可以轻松地使用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)
}
© www.soinside.com 2019 - 2024. All rights reserved.