亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 NewSingleHostReverseProxy 處理錯誤

如何使用 NewSingleHostReverseProxy 處理錯誤

Go
繁花如伊 2023-07-04 09:59:25
我正在嘗試做一個負載均衡器來研究一些 go 包。我想在請求超時或給出 404 錯誤時處理錯誤,但找不到如何執行此操作。func main() {    // start server    http.HandleFunc("/", handleRequestAndRedirect)    if err := http.ListenAndServe(getListenAddress(), nil); err != nil {        panic(err)    }}func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {    ur, _ := url.Parse("https://www.instagram.com/")    proxy := httputil.NewSingleHostReverseProxy(ur)    // Update the headers to allow for SSL redirection    req.URL.Host = ur.Host    req.URL.Scheme = ur.Scheme    req.Header.Set("X-Forwarded-Host", req.Header.Get("Host"))    req.Host = ur.Host    req.Header.Set("Key", "Teste")    proxy.ServeHTTP(res, req)}
查看完整描述

2 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

當我最終在這里尋找一種處理來自代理主機的 404 錯誤的方法時,我想補充已接受的答案,如果它對登陸此頁面的人有任何幫助的話。

正如官方文檔(https://golang.org/pkg/net/http/httputil/#ReverseProxy)中所述:

ModifyResponse 是一個可選函數,用于修改來自后端的 Response。如果后端返回帶有任何 HTTP 狀態代碼的響應,則會調用它。如果后端無法訪問,則調用可選的ErrorHandler,而不調用任何ModifyResponse。如果ModifyResponse返回錯誤,則使用其錯誤值調用ErrorHandler。如果 ErrorHandler 為 nil,則使用其默認實現。

因此,如果您不僅想捕獲“真實”錯誤(主機無法訪問),還想捕獲來自服務器的錯誤響應代碼(404、500...),您應該使用檢查響應狀態代碼并返回錯誤,這將ModifyResponse是然后被你的ErrorHandler函數捕獲。接受的答案示例變為:

func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {

    

    ur, _ := url.Parse("https://www.instagram.com/")

    proxy := httputil.NewSingleHostReverseProxy(ur)

    // Update the headers to allow for SSL redirection

    req.URL.Host = ur.Host

    req.URL.Scheme = ur.Scheme

    req.Header.Set("X-Forwarded-Host", req.Header.Get("Host"))

    req.Host = ur.Host

    req.Header.Set("Key", "Teste")


    proxy.ErrorHandler = ErrHandle

    proxy.ModifyResponse = ModifyResponse

    proxy.ServeHTTP(res, req)

}


func ModifyResponse(res *http.Response) error {

    if res.StatusCode == 404 {

        return errors.New("404 error from the host")

    }

    return nil

}  


func ErrHandle(res http.ResponseWriter, req *http.Request, err error) {

    fmt.Println(err)

}  


查看完整回答
反對 回復 2023-07-04
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

使用 proxy.ErrorHandler


ErrorHandler func(http.ResponseWriter, *http.Request, 錯誤)


  func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {


        ur, _ := url.Parse("https://www.instagram.com/")

        proxy := httputil.NewSingleHostReverseProxy(ur)

        // Update the headers to allow for SSL redirection

        req.URL.Host = ur.Host

        req.URL.Scheme = ur.Scheme

        req.Header.Set("X-Forwarded-Host", req.Header.Get("Host"))

        req.Host = ur.Host

        req.Header.Set("Key", "Teste")


        proxy.ErrorHandler = ErrHandle

        proxy.ServeHTTP(res, req)

    }


    func ErrHandle(res http.ResponseWriter, req *http.Request, err error) {

        fmt.Println(err)

    }     


查看完整回答
反對 回復 2023-07-04
  • 2 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號