<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Go語(yǔ)言正/反向代理的姿勢(shì)

          共 4202字,需瀏覽 9分鐘

           ·

          2022-11-09 11:53

          先重溫一下什么叫反向代理,正向代理。

          鵝廠二面,nginx回憶錄[1]

          所謂正向,反向代理取決于代理的是出站請(qǐng)求,還是入站請(qǐng)求

          正向代理:代理的出站請(qǐng)求, 客戶端能感知到代理程序,架構(gòu)上距離客戶端更近。
          反向代理:代理的是入站請(qǐng)求,客戶端認(rèn)為代理程序就是服務(wù)器,客戶端感知不到代理邏輯,架構(gòu)上距離服務(wù)端更近。


          前幾天利用golang實(shí)現(xiàn)反向代理程序[2],引出了Host請(qǐng)求頭在反代中的關(guān)鍵作用。

          代理程序預(yù)置upstream, 將節(jié)點(diǎn)對(duì)proxy.com的請(qǐng)求,轉(zhuǎn)發(fā)給upstream service。
          package main

          import (
              "fmt"
              "log"
              "net/http"
              "net/http/httputil"
          )
          // 將對(duì)proxy.com/xxx/yyy的請(qǐng)求轉(zhuǎn)發(fā)到 www.baidu.com/xxx/yyy
          func ReverseProxyHandler(w http.ResponseWriter, r *http.Request) {
              fmt.Println("receive a request from:", r.RemoteAddr, r.Header)

              target := "www.baidu.com"
              director := func(req *http.Request) {
                  req.URL.Scheme = "https"
                  req.URL.Host = target
                  req.Host = target
              }

              proxy := &httputil.ReverseProxy{Director: director}
              proxy.ServeHTTP(w, r)
          }

          func main() {
              fmt.Printf("Starting server at port 8080\n")
              if err := http.ListenAndServe(":8080", http.HandlerFunc(ReverseProxyHandler)); err != nil {
                  log.Fatal(err)
              }
          }

          以上訪問(wèn)localhost:8080, 在瀏覽器會(huì)顯示www.baidu.com的內(nèi)容。

          --------------------------------------------------------

          這幾天剛好遇到了一個(gè)正向代理的case, 簡(jiǎn)單記錄一下。

          name.com集群 通過(guò)prxoy.com 代理程序 請(qǐng)求外部節(jié)點(diǎn)。
          package main

          import (
              "fmt"
              "log"
              "net/http"
              "net/http/httputil"
          )
          // 集群請(qǐng)求外部節(jié)點(diǎn),通過(guò)代理
          func ProxyHandler(w http.ResponseWriter, r *http.Request) {
              fmt.Printf("receive a request from {0}  {1}: \n", r.RemoteAddr, r.Header)
              if r.Host != "localhost:8080" {
                  director := func(req *http.Request) {
                      req.URL.Scheme = "http"
                      req.URL.Host = r.Host
                      req.Host = r.Host
                  }
                  proxy := &httputil.ReverseProxy{Director: director}
                  proxy.ServeHTTP(w, r)
              } else {
                  http.NotFound(w, r)
              }

          }

          func main() {
              if err := http.ListenAndServe(":8080", http.HandlerFunc(ProxyHandler)); err != nil {
                  log.Fatal(err)
              }
          }

          其中要注意的就是,正向代理式要規(guī)避死循環(huán)代理。

          使用該服務(wù)作為代理程序,將可以出站訪問(wèn)任何地址( curl -x 指令后面代理地址)。

          使用時(shí),針對(duì)httpclient 設(shè)置proxy。

          //adding the proxy settings to the Transport object
              transport := &http.Transport{
                  Proxy: http.ProxyURL(proxyURL),
              }

              //adding the Transport object to the http Client
              client := &http.Client{
                  Transport: transport,
              }

          下面使用curl指令快速驗(yàn)證:  curl -x 127.0.0.1:8080 www.baidu.com

          引用鏈接

          [1] 鵝廠二面,nginx回憶錄: https://www.cnblogs.com/JulianHuang/p/14504892.html
          [2] golang實(shí)現(xiàn)反向代理程序: https://www.cnblogs.com/JulianHuang/p/16639016.html

          本文文字和制圖均為原創(chuàng),鄙人會(huì)不時(shí)更正認(rèn)知、修正理解,鑒于公眾號(hào)編輯次數(shù)受限,請(qǐng)不時(shí)關(guān)注左下角永久更新地址, 也歡迎斧正;如果對(duì)您有所幫助,一鍵三連,甚是欣慰。 

          瀏覽 72
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲AV成人中文无码专区观看 | 五月婷婷五月丁香 | 免费69视频 | 精品人妻系列 | 人人摸人人操人人奶 |