Example of hosting multiple domains in a single Golang HTTP server.
To host multiple domains in a single Golang HTTP server, we need to use Host-specific pattern for ServeMux:
Patterns may optionally begin with a host name, restricting matches to URLs on that host only. Host-specific patterns take precedence over general patterns, so that a handler might register for the two patterns "/codesearch" and "codesearch.google.com/" without also taking over requests for "http://www.google.com/".
// For Example:
func main() {
http.HandleFunc("a.com/", helloA)
http.HandleFunc("sub.a.com/", helloSubA)
http.HandleFunc("b.com/", helloB)
if err := http.ListenAndServe(":80", nil); err != nil {
log.Fatal(err)
}
}
- Add "a.com", "sub.a.com", "b.com" into
/etc/hosts
- Restart network service or reboot.
sudo ./go-host-multi-domains-example
- Run
curl a.com
,curl sub.a.com
,curl b.com
to test.