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

Bes*_*nna 3 google-cloud-platform google-cloud-run

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

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

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

pet*_*ina 5

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)
}
Run Code Online (Sandbox Code Playgroud)