1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 11:43:41 +00:00
fiber/middleware/proxy/README.md

139 lines
3.1 KiB
Markdown
Raw Normal View History

2020-09-13 11:20:11 +02:00
# Proxy
2020-10-22 11:28:35 +02:00
2020-09-27 18:24:05 +02:00
Proxy middleware for [Fiber](https://github.com/gofiber/fiber) that allows you to proxy requests to multiple servers.
2020-09-13 11:20:11 +02:00
### Table of Contents
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
### Signatures
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
```go
2020-09-27 18:24:05 +02:00
func Balancer(config Config) fiber.Handler
func Forward(addr string) fiber.Handler
2020-10-22 11:28:35 +02:00
func Do(c *fiber.Ctx, addr string) error
2020-09-13 11:20:11 +02:00
```
### Examples
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
Import the middleware package that is part of the Fiber web framework
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
```go
import (
2020-09-27 18:24:05 +02:00
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
2020-09-13 11:20:11 +02:00
)
```
After you initiate your Fiber app, you can use the following possibilities:
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
```go
// if target https site uses a self-signed certificate, you should
// call WithTlsConfig before Do and Forward
proxy.WithTlsConfig(&tls.Config{
InsecureSkipVerify: true,
})
2020-09-27 18:24:05 +02:00
// Forward to url
app.Get("/gif", proxy.Forward("https://i.imgur.com/IWaBepg.gif"))
// Make request within handler
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
}
// Remove Server header from response
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
// Minimal round robin balancer
app.Use(proxy.Balancer(proxy.Config{
Servers: []string{
"http://localhost:3001",
"http://localhost:3002",
"http://localhost:3003",
},
2020-09-13 11:20:11 +02:00
}))
2020-09-27 18:24:05 +02:00
// Or extend your balancer for customization
app.Use(proxy.Balancer(proxy.Config{
Servers: []string{
"http://localhost:3001",
"http://localhost:3002",
"http://localhost:3003",
},
ModifyRequest: func(c *fiber.Ctx) error {
2020-10-22 11:28:35 +02:00
c.Request().Header.Add("X-Real-IP", c.IP())
2020-09-13 11:20:11 +02:00
return nil
},
2020-09-27 18:24:05 +02:00
ModifyResponse: func(c *fiber.Ctx) error {
c.Response().Header.Del(fiber.HeaderServer)
return nil
},
2020-09-13 11:20:11 +02:00
}))
```
### Config
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
```go
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
2020-09-27 18:24:05 +02:00
// Servers defines a list of <scheme>://<host> HTTP servers,
2020-09-13 11:20:11 +02:00
//
2020-09-27 18:24:05 +02:00
// which are used in a round-robin manner.
// i.e.: "https://foobar.com, http://www.foobar.com"
2020-09-13 11:20:11 +02:00
//
2020-09-27 18:24:05 +02:00
// Required
Servers []string
2020-09-13 11:20:11 +02:00
2020-09-27 18:24:05 +02:00
// ModifyRequest allows you to alter the request
//
// Optional. Default: nil
ModifyRequest fiber.Handler
2020-09-13 11:20:11 +02:00
2020-09-27 18:24:05 +02:00
// ModifyResponse allows you to alter the response
//
// Optional. Default: nil
ModifyResponse fiber.Handler
// Timeout is the request timeout used when calling the proxy client
//
// Optional. Default: 1 second
Timeout time.Duration
// Per-connection buffer size for requests' reading.
// This also limits the maximum header size.
// Increase this buffer if your clients send multi-KB RequestURIs
// and/or multi-KB headers (for example, BIG cookies).
ReadBufferSize int
// Per-connection buffer size for responses' writing.
WriteBufferSize int
// tls config for the http client
TlsConfig *tls.Config
2020-09-13 11:20:11 +02:00
}
```
### Default Config
2020-10-22 11:28:35 +02:00
2020-09-13 11:20:11 +02:00
```go
2020-09-27 18:24:05 +02:00
// ConfigDefault is the default config
2020-09-13 11:20:11 +02:00
var ConfigDefault = Config{
Next: nil,
ModifyRequest: nil,
ModifyResponse: nil,
Timeout: fasthttp.DefaultLBClientTimeout,
2020-09-13 11:20:11 +02:00
}
```