1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 04:03:45 +00:00
2020-09-14 12:34:31 +02:00
..
2020-09-14 12:34:31 +02:00
v2
2020-09-13 11:20:11 +02:00
v2
2020-09-13 11:20:11 +02:00

Proxy

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

Table of Contents

Signatures

func New(config Config) fiber.Handler

Examples

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

import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/proxy"
)

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

// Minimal config
app.Use(proxy.New(proxy.Config{
	Hosts: "gofiber.io:8080, gofiber.io:8081",
}))

// Or extend your config for customization
app.Use(proxy.New(proxy.Config{
	Hosts: "gofiber.io:8080, gofiber.io:8081",
	Before: func(c *fiber.Ctx) error {
		c.Set("X-Real-IP", c.IP())
		return nil
	},
}))

Config

// 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

	// Comma-separated list of upstream HTTP server host addresses,
	// which are passed to Dial in a round-robin manner.
	//
	// Each address may contain port if default dialer is used.
	// For example,
	//
	//    - foobar.com:80
	//    - foobar.com:443
	//    - foobar.com:8080
	Hosts string

	// Before allows you to alter the request
	Before fiber.Handler

	// After allows you to alter the response
	After fiber.Handler
}

Default Config

var ConfigDefault = Config{
	Next:   nil,
	Hosts:  "",
	Before: nil,
	After:  nil,
}