Proxy
Fiber를 위한 프록시 미들웨어로, 여러 서버로 요청을 프록시할 수 있습니다.
Signatures
// Balancer는 여러 업스트림 서버 간에 로드 밸런서를 생성합니다.
func Balancer(config Config) fiber.Handler
// Forward는 주어진 http 요청을 수행하고 주어진 http 응답을 채웁니다.
func Forward(addr string, clients ...*fasthttp.Client) fiber.Handler
// Do는 주어진 http 요청을 수행하고 주어진 http 응답을 채웁니다.
func Do(c *fiber.Ctx, addr string, clients ...*fasthttp.Client) error
// DoRedirects는 주어진 http 요청을 수행하고 maxRedirectsCount 리디렉션까지 따라가며 주어진 http 응답을 채웁니다.
func DoRedirects(c *fiber.Ctx, addr string, maxRedirectsCount int, clients ...*fasthttp.Client) error
// DoDeadline은 주어진 요청을 수행하고 주어진 데드라인까지 응답을 기다립니다.
func DoDeadline(c *fiber.Ctx, addr string, deadline time.Time, clients ...*fasthttp.Client) error
// DoTimeout은 주어진 요청을 수행하고 주어진 타임아웃 기간 동안 응답을 기다립니다.
func DoTimeout(c *fiber.Ctx, addr string, timeout time.Duration, clients ...*fasthttp.Client) error
// DomainForward는 주어진 도메인을 기반으로 주어진 http 요청을 수행하고 주어진 http 응답을 채웁니다.
func DomainForward(hostname string, addr string, clients ...*fasthttp.Client) fiber.Handler
// BalancerForward는 라운드 로빈 밸런서를 기반으로 주어진 http 요청을 수행하고 주어진 http 응답을 채웁니다.
func BalancerForward(servers []string, clients ...*fasthttp.Client) fiber.Handler
Examples
Fiber 웹 프레임워크의 일부인 미들웨어 패키지를 가져옵니다.
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
)
Fiber 앱을 초기화한 후에는 다음과 같은 가능성을 사용할 수 있습니다:
// 대상 https 사이트가 자체 서명된 인증서를 사용하는 경우 Do와 Forward 전에 WithTlsConfig를 호출해야 합니다.
proxy.WithTlsConfig(&tls.Config{
InsecureSkipVerify: true,
})
// 전역 자체 사용자 지정 클라이언트를 사용해야 하는 경우 proxy.WithClient를 사용해야 합니다.
proxy.WithClient(&fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
})
// URL로 포워드
app.Get("/gif", proxy.Forward("https://i.imgur.com/IWaBepg.gif"))
// 특정 도메인으로 포워드하려면 proxy.DomainForward를 사용해야 합니다.
app.Get("/payments", proxy.DomainForward("docs.gofiber.io", "http://localhost:8000"))
// 로컬 사용자 지정 클라이언트를 사용하여 URL로 포워드
app.Get("/gif", proxy.Forward("https://i.imgur.com/IWaBepg.gif", &fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
}))
// 핸들러 내에서 요청 만들기
app.Get("/:id", func(c *fiber.Ctx) error {
url := "https://i.imgur.com/"+c.Params("id")+".gif"
if err := proxy.Do(c, url); err != nil {
return err
}
// 응답에서 Server 헤더 제거
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
// 리디렉션을 따르는 동안 프록시 요청 만들기
app.Get("/proxy", func(c *fiber.Ctx) error {
if err := proxy.DoRedirects(c, "http://google.com", 3); err != nil {
return err
}
// 응답에서 Server 헤더 제거
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
// 프록시 요청을 만들고 최대 5초까지 기다린 후 시간 초과
app.Get("/proxy", func(c *fiber.Ctx) error {
if err := proxy.DoTimeout(c, "http://localhost:3000", time.Second * 5); err != nil {
return err
}
// 응답에서 Server 헤더 제거
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
// 프록시 요청을 만들고 지금으로부터 1분 후에 시간 초과
app.Get("/proxy", func(c *fiber.Ctx) error {
if err := proxy.DoDeadline(c, "http://localhost", time.Now().Add(time.Minute)); err != nil {
return err
}
// 응답에서 Server 헤더 제거
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
// 최소한의 라운드 로빈 밸런서
app.Use(proxy.Balancer(proxy.Config{
Servers: []string{
"http://localhost:3001",
"http://localhost:3002",
"http://localhost:3003",
},
}))
// 또는 사용자 지정을 위해 밸런서 확장
app.Use(proxy.Balancer(proxy.Config{
Servers: []string{
"http://localhost:3001",
"http://localhost:3002",
"http://localhost:3003",
},
ModifyRequest: func(c *fiber.Ctx) error {
c.Request().Header.Add("X-Real-IP", c.IP())
return nil
},
ModifyResponse: func(c *fiber.Ctx) error {
c.Response().Header.Del(fiber.HeaderServer)
return nil
},
}))
// 또는 밸런서가 https를 사용하고 대상 서버는 http만 사용하는 경우 이 방법을 사용합니다.
app.Use(proxy.BalancerForward([]string{
"http://localhost:3001",
"http://localhost:3002",
"http://localhost:3003",
}))
Config
Next
func(*fiber.Ctx) bool
Next는 true를 반환할 때 이 미들웨어를 건너뛰는 함수를 정의합니다.
nil
Servers
[]string
Servers는 라운드 로빈 방식으로 사용되는 <scheme>://<host>
HTTP 서버 목록을 정의합니다. 예: "https://foobar.com, http://www.foobar.com"
(필수)
ModifyRequest
fiber.Handler
ModifyRequest를 사용하면 요청을 변경할 수 있습니다.
nil
ModifyResponse
fiber.Handler
ModifyResponse를 사용하면 응답을 변경할 수 있습니다.
nil
Timeout
time.Duration
Timeout은 프록시 클라이언트를 호출할 때 사용되는 요청 시간 초과입니다.
1초
ReadBufferSize
int
요청 읽기를 위한 연결당 버퍼 크기입니다. 이는 최대 헤더 크기도 제한합니다. 클라이언트가 다중 KB RequestURI 및/또는 다중 KB 헤더(예: 큰 쿠키)를 보내는 경우 이 버퍼를 늘리세요.
(지정되지 않음)
WriteBufferSize
int
응답 쓰기를 위한 연결당 버퍼 크기입니다.
(지정되지 않음)
TlsConfig
*tls.Config
(또는 v3에서는 *fasthttp.TLSConfig
)
HTTP 클라이언트에 대한 TLS 구성입니다.
nil
Client
*fasthttp.LBClient
Client는 클라이언트 구성이 복잡할 때 사용되는 사용자 지정 클라이언트입니다.
nil
Default Config
var ConfigDefault = Config{
Next: nil,
ModifyRequest: nil,
ModifyResponse: nil,
Timeout: fasthttp.DefaultLBClientTimeout,
}
Last updated