相关疑难解决方法(0)

如何在Go中重写/重定向从http到https?

我已经提出了TLS并且它有效.我知道如何在nginx中从http重写为https,但我不再使用nginx了.我不知道如何在Go中正确地做到这一点.

func main() {

    certificate := "/srv/ssl/ssl-bundle.crt"
    privateKey := "/srv/ssl/mykey.key"

    http.HandleFunc("/", rootHander)
    // log.Fatal(http.ListenAndServe(":80", nil))
    log.Fatal(http.ListenAndServeTLS(":443", certificate, privateKey, nil))
}

func rootHander(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("To the moon!"))
}
Run Code Online (Sandbox Code Playgroud)

我怎么能以这种方式做到这一点?

go

16
推荐指数
2
解决办法
5539
查看次数

设置 Let's encrypt with Go - 握手错误

我正在尝试在用 Go 编写的负载均衡器上设置让我们加密,我尝试了自动和手动设置,但总是出错。

该域正确指向我们的服务器(Digital Ocean),我什至可以从浏览器中打开该站点而不会出错,而且 ssl 检查也没有报告此域上的错误。事实是,当我从 CLI 在服务器上运行 Go 可执行文件时,我反复出现错误。

  1. 自动(acme/autocert)设置:

服务器代码是,服务器启动后第一次从浏览器查看域时创建的证书和密钥:

    go func() {
        log.Printf("Staring HTTP service on %s ...", ":80")

        http.HandleFunc("/*", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
            http.Redirect(w, r, "https://" + app.Cfg.S_HOST + ":443" + r.RequestURI, http.StatusMovedPermanently)
        }))

        if err := http.ListenAndServe(":80", nil); err != nil {
            errs <- err
        }

    }()



    log.Printf("Staring HTTPS service on %s ...", ":443")

    http.HandleFunc("/hello", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/plain")
        w.Write([]byte("This is an example server.\n"))
    }))


    certManager := autocert.Manager{ …
Run Code Online (Sandbox Code Playgroud)

ssl ssl-certificate go lets-encrypt

5
推荐指数
1
解决办法
4818
查看次数

标签 统计

go ×2

lets-encrypt ×1

ssl ×1

ssl-certificate ×1