Proxy

Proxy middleware for Fiber that allows you to proxy requests to multiple hosts.

Signatures

  1. func New(config Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/proxy"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Minimal config
  2. app.Use(proxy.New(proxy.Config{
  3. Hosts: "gofiber.io:8080, gofiber.io:8081",
  4. }))
  5. // Or extend your config for customization
  6. app.Use(proxy.New(proxy.Config{
  7. Hosts: "gofiber.io:8080, gofiber.io:8081",
  8. Before: func(c *fiber.Ctx) error {
  9. c.Set("X-Real-IP", c.IP())
  10. return nil
  11. },
  12. }))

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // Comma-separated list of upstream HTTP server host addresses,
  8. // which are passed to Dial in a round-robin manner.
  9. //
  10. // Each address may contain port if default dialer is used.
  11. // For example,
  12. //
  13. // - foobar.com:80
  14. // - foobar.com:443
  15. // - foobar.com:8080
  16. Hosts string
  17. // Before allows you to alter the request
  18. Before fiber.Handler
  19. // After allows you to alter the response
  20. After fiber.Handler
  21. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. Hosts: "",
  4. Before: nil,
  5. After: nil,
  6. }