第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Go 中從 http 重寫/重定向到 https?

如何在 Go 中從 http 重寫/重定向到 https?

Go
手掌心 2022-01-17 10:09:41
我已經(jīng)建立了 TLS 并且它有效。我知道如何在 nginx 中從 http 重寫為 https,但我不再使用 nginx。我不知道如何在 Go 中正確執(zhí)行此操作。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!"))}我將如何以一種好的方式做到這一點(diǎn)?
查看完整描述

3 回答

?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

創(chuàng)建一個(gè)處理重定向到 https 的處理程序,例如:


func redirectTLS(w http.ResponseWriter, r *http.Request) {

    http.Redirect(w, r, "https://IPAddr:443"+r.RequestURI, http.StatusMovedPermanently)

}

然后重定向http流量:


go func() {

    if err := http.ListenAndServe(":80", http.HandlerFunc(redirectTLS)); err != nil {

        log.Fatalf("ListenAndServe error: %v", err)

    }

}()


查看完整回答
反對(duì) 回復(fù) 2022-01-17
?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

上面發(fā)布的解決方案有點(diǎn)不靈活,尤其是在外部主機(jī)名與本地主機(jī)不同的情況下。


這是我用于 HTTP->HTTPS 重定向的代碼:


package main


import (

    "net"

    "log"

    "net/http"

)


var httpAddr ":8080"

var httpsAddr ":8443"


func main() {

    srv := http.Server{

        Addr: httpsAddr,

    }


    _, tlsPort, err := net.SplitHostPort(httpsAddr)

    if err != nil {

        return err

    }

    go redirectToHTTPS(tlsPort)


    srv.ListenAndServeTLS("cert.pem", "key.pem")

}


func redirectToHTTPS(tlsPort string) {

    httpSrv := http.Server{

        Addr: httpAddr,

        Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){

            host, _, _ := net.SplitHostPort(r.Host)

            u := r.URL

            u.Host = net.JoinHostPort(host, tlsPort)

            u.Scheme="https"

            log.Println(u.String())

            http.Redirect(w,r,u.String(), http.StatusMovedPermanently)

        }),

    }

    log.Println(httpSrv.ListenAndServe())

}

如果您使用標(biāo)準(zhǔn)端口 (80,443),則不需要拆分地址連接,只需在 URL 上設(shè)置方案就足夠了。


查看完整回答
反對(duì) 回復(fù) 2022-01-17
?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

package main


import (

    "fmt"

    "net/http"

)


func redirectToHttps(w http.ResponseWriter, r *http.Request) {

    // Redirect the incoming HTTP request. Note that "127.0.0.1:443" will only work if you are accessing the server from your local machine.

    http.Redirect(w, r, "https://127.0.0.1:443"+r.RequestURI, http.StatusMovedPermanently)

}


func handler(w http.ResponseWriter, r *http.Request) {

    fmt.Fprintf(w, "Hi there!")

    fmt.Println(r.RequestURI)

}


func main() {

    http.HandleFunc("/", handler)

    // Start the HTTPS server in a goroutine

    go http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)

    // Start the HTTP server and redirect all incoming connections to HTTPS

    http.ListenAndServe(":8080", http.HandlerFunc(redirectToHttps))

}


查看完整回答
反對(duì) 回復(fù) 2022-01-17
  • 3 回答
  • 0 關(guān)注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)