Skip to content

Instantly share code, notes, and snippets.

@alapini
Forked from seblegall/reverseproxy.go
Created July 11, 2025 16:32
Show Gist options
  • Select an option

  • Save alapini/cb6470dccef8b45f9563bc9008b24133 to your computer and use it in GitHub Desktop.

Select an option

Save alapini/cb6470dccef8b45f9563bc9008b24133 to your computer and use it in GitHub Desktop.
A reverse proxy in Golang using Gin
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"github.com/gin-gonic/gin"
)
func proxy(c *gin.Context) {
remote, err := url.Parse("http://myremotedomain.com")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = c.Param("proxyPath")
}
proxy.ServeHTTP(c.Writer, c.Request)
}
func main() {
r := gin.Default()
r.Any("/*proxyPath", proxy)
r.Run(":8080")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment