Last active
November 10, 2024 08:08
-
-
Save seblegall/2a2697fc56417b24a7ec49eb4a8d7b1b to your computer and use it in GitHub Desktop.
A reverse proxy in Golang using Gin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
Thank you very much <3
Getting lot of Bad Gateway issue and [EOF](httputil: ReverseProxy read error during body copy: unexpected EOF) error in it
Thank you.
PS: This also works with websockets
Works like a charm 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very very much to your help.