I have a Go server-side program hosted on an Ubuntu 18.04 server. I want to handle example.com
with Go, then example.com/blog
or blog.example.com
with PHP in shared hosting. I prefer to do this with Go itself, not with another web server (ex: Nginx), How can I do this?
You need to handle the endpoints served by another server/process as a reverse proxy. The standard library gives you httputil.ReverseProxy, which acts as a standard http.Handler
, to make this very easy.
Try something like this:
target, err := url.Parse("http://blog.example.com/")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
mux.Handle("/blog", proxy)