dongqiang1226 2017-05-02 23:56
浏览 25
已采纳

Google Cloud Platform,Golang弹性环境HTTPS仅适用于自订网域

I have deployed my application without hassle just by running gcloud app deploy command on GCP. Which takes the flexible environment as default. Yesterday I made the necessary customizations to have a custom domain for this app with ssl. Currently it works when I go to any of the following, http://example.com, https://example.com but I also want to force people to use https. Currently the http requests work as they are, I want them to be directed to https. I want to direct any user to https://example.com when they try to go to the website with http or without anything at all like example.com. How can this be achieved?

Here's my app.yaml:

api_version: go1
env: flex
runtime: go

I already tried to use handlers and secure attributes but it seems they are not valid for flexible environment.

Thanks.

  • 写回答

1条回答 默认 最新

  • duandingyou3331 2017-05-18 16:41
    关注

    Currently, flexible environment does not support HTTPS only directing by using app.yaml. However, this can be achieved in the server code by using a function like this,

    func directToHttps(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
        if r.URL.Scheme == "https" || strings.HasPrefix(r.Proto, "HTTPS") || r.Header.Get("X-Forwarded-Proto") == "https" {
            next(w, r)
        } else {
            target := "https://" + r.Host + r.URL.Path
    
            http.Redirect(w, r, target,
                http.StatusTemporaryRedirect)
        }
    }
    

    I wrapped this function to my handlers with negroni.

    Working example can be found here: https://github.com/malisit/munhasir

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题