Skip to content

Instantly share code, notes, and snippets.

@JalfResi
Last active August 9, 2025 19:56
Show Gist options
  • Select an option

  • Save JalfResi/6287706 to your computer and use it in GitHub Desktop.

Select an option

Save JalfResi/6287706 to your computer and use it in GitHub Desktop.
Simple reverse proxy in Go
package main
import(
"log"
"net/url"
"net/http"
"net/http/httputil"
)
func main() {
remote, err := url.Parse("http://google.com")
if err != nil {
panic(err)
}
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
r.Host = remote.Host
w.Header().Set("X-Ben", "Rad")
p.ServeHTTP(w, r)
}
}
proxy := httputil.NewSingleHostReverseProxy(remote)
http.HandleFunc("/", handler(proxy))
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
@semanticart

Copy link
Copy Markdown

This is great. Thanks!

@filewalkwithme

Copy link
Copy Markdown

awesome!!!

@elico

elico commented Aug 10, 2015

Copy link
Copy Markdown

Thanks!!

@johnandersen777

Copy link
Copy Markdown

👍 :octocat:

@thurt

thurt commented Jan 19, 2018

Copy link
Copy Markdown

instead of using a closure, you may find creating a struct instance to be simpler. see my fork https://gist.github.com/thurt/2ae1be5fd12a3501e7f049d96dc68bb9 i think it is more idiomatic for golang

@WhyBeingAsianRapperIsSoHard

Copy link
Copy Markdown

Thx

@ijt

ijt commented Feb 24, 2020

Copy link
Copy Markdown

I tried this and it came up 404 in Chrome and Safari.

@xdkxlk

xdkxlk commented Mar 13, 2020

Copy link
Copy Markdown

How to control the http timeout?

@BeforyDeath

Copy link
Copy Markdown

added r.Host = remote.Host

@nilskch

nilskch commented May 22, 2020

Copy link
Copy Markdown

I received a 404 as well. Does anyone have a solution?

@kubosuke

kubosuke commented Jun 2, 2020

Copy link
Copy Markdown

lovely!

@sokryptk

Copy link
Copy Markdown

This returns a 404, any solutions?

ghost commented Sep 3, 2020

Copy link
Copy Markdown

Does it forward the client IP?

@zack-wang

Copy link
Copy Markdown

Excellent

@dudigit

dudigit commented Apr 3, 2021

Copy link
Copy Markdown

Any solution to 404 ?

@JalfResi

Copy link
Copy Markdown
Author

Added "r.Host = remote.Host" to fix 404 issues

@lwzm

lwzm commented Jul 16, 2021

Copy link
Copy Markdown

Added "r.Host = remote.Host" to fix 404 issues

undeclared name: remote

function handler should be placed in function main:

	handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
		return func(w http.ResponseWriter, r *http.Request) {
			log.Println(r.URL)
			r.Host = remote.Host
			w.Header().Set("X-Ben", "Rad")
			p.ServeHTTP(w, r)
		}
	}

@JalfResi

Copy link
Copy Markdown
Author

Thanks @lwzm!

@abdennour

Copy link
Copy Markdown

Great!

@doolgibi

Copy link
Copy Markdown

hi, after saved this file, how can i try?

@samuelnihoul

Copy link
Copy Markdown

go run main.go
then
curl localhost:8080
I think

ghost commented Dec 22, 2022

Copy link
Copy Markdown

Made some changes to add support for ssl. And cleanup handler.

`package main

import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"crypto/tls"
)

func main() {
// Replace 'target' with the URL of the server you want to proxy to
target, err := url.Parse("https://example.com")
if err != nil {
panic(err)
}

// Create a new ReverseProxy instance
proxy := httputil.NewSingleHostReverseProxy(target)
// Configure the reverse proxy to use HTTPS
proxy.Transport = &http.Transport{
    TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Create a handler function that logs the URL and forwards the request to the proxy
handler := func(w http.ResponseWriter, r *http.Request) {
    log.Println(r.URL)
    r.Host = target.Host
	//w.Header().Set("X-Ben", "radi")
    proxy.ServeHTTP(w, r)
}

// Register the handler function with the HTTP server
http.HandleFunc("/", handler)

// Start the HTTP server
err = http.ListenAndServe(":3000", nil)
if err != nil {
    panic(err)
}

}`

ghost commented Dec 22, 2022

Copy link
Copy Markdown

@rjp

rjp commented May 24, 2024

Copy link
Copy Markdown

Helpful, ta, used this to transparently adjust some query parameters for certain requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment