What is a Reverse Proxy?

A reverse proxy sits between clients and your application servers, directing requests to the appropriate backend while providing additional features like load balancing and SSL termination.

Basic Implementation


package main

import (
    "net/http"
    "net/http/httputil"
    "net/url"
)

type Server struct {
    addr  string
    proxy *httputil.ReverseProxy
}

func NewServer(addr string) *Server {
    url, _ := url.Parse(addr)
    return &Server{
        addr:  addr,
        proxy: httputil.NewSingleHostReverseProxy(url),
    }
}

func main() {
    servers := []*Server{
        NewServer("http://localhost:8081"),
        NewServer("http://localhost:8082"),
    }
    // Implementation continues...
}