GO語言"net/http/httputil"包中"ReverseProxy"類型的用法及代碼示例。
ReverseProxy 是一個 HTTP 處理程序,它接受傳入的請求並將其發送到另一台服務器,將響應代理回客戶端。
ReverseProxy 默認將客戶端 IP 設置為 X-Forwarded-For 標頭的值。
如果 X-Forwarded-For 標頭已存在,則客戶端 IP 將附加到現有值。作為一種特殊情況,如果標頭存在於 Request.Header 映射中但具有 nil 值(例如由 Director func 設置時),則不會修改 X-Forwarded-For 標頭。
為防止 IP 欺騙,請務必刪除來自客戶端或不受信任的代理的任何預先存在的 X-Forwarded-For 標頭。
用法:
type ReverseProxy struct {
// Director must be a function which modifies
// the request into a new request to be sent
// using Transport.Its response is then copied
// back to the original client unmodified.
// Director must not access the provided Request
// after returning.
Director func(*http.Request)
// The transport used to perform proxy requests.
// If nil, http.DefaultTransport is used.
Transport http.RoundTripper
// FlushInterval specifies the flush interval
// to flush to the client while copying the
// response body.
// If zero, no periodic flushing is done.
// A negative value means to flush immediately
// after each write to the client.
// The FlushInterval is ignored when ReverseProxy
// recognizes a response as a streaming response, or
// if its ContentLength is -1; for such responses, writes
// are flushed to the client immediately.
FlushInterval time.Duration
// ErrorLog specifies an optional logger for errors
// that occur when attempting to proxy the request.
// If nil, logging is done via the log package's standard logger.
ErrorLog *log.Logger // Go 1.4
// BufferPool optionally specifies a buffer pool to
// get byte slices for use by io.CopyBuffer when
// copying HTTP response bodies.
BufferPool BufferPool // Go 1.6
// ModifyResponse is an optional function that modifies the
// Response from the backend.It is called if the backend
// returns a response at all, with any HTTP status code.
// If the backend is unreachable, the optional ErrorHandler is
// called without any call to ModifyResponse.
//
// If ModifyResponse returns an error, ErrorHandler is called
// with its error value.If ErrorHandler is nil, its default
// implementation is used.
ModifyResponse func(*http.Response) error // Go 1.8
// ErrorHandler is an optional function that handles errors
// reaching the backend or errors from ModifyResponse.
//
// If nil, the default is to log the provided error and return
// a 502 Status Bad Gateway response.
ErrorHandler func(http.ResponseWriter, *http.Request, error) // Go 1.11
}
例子:
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
)
func main() {
backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "this call was relayed by the reverse proxy")
}))
defer backendServer.Close()
rpURL, err := url.Parse(backendServer.URL)
if err != nil {
log.Fatal(err)
}
frontendProxy := httptest.NewServer(httputil.NewSingleHostReverseProxy(rpURL))
defer frontendProxy.Close()
resp, err := http.Get(frontendProxy.URL)
if err != nil {
log.Fatal(err)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", b)
}
輸出:
this call was relayed by the reverse proxy
相關用法
- GO ReverseBytes64用法及代碼示例
- GO ReverseBytes16用法及代碼示例
- GO Reverse用法及代碼示例
- GO Reverse8用法及代碼示例
- GO Reverse32用法及代碼示例
- GO Reverse64用法及代碼示例
- GO Reverse16用法及代碼示例
- GO ReverseBytes32用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO ReadMessage用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO ResponseWriter用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Read用法及代碼示例
- GO Rel用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 ReverseProxy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。