Skip to main content

Host Multiple Web Sites of Go Using Reverse Proxy Example




Go supports built-in server. But there is a limitation that you can host only one site at once with one port.

Though you can host multiple sites with the domain name using gorilla mux then all site will come with one app that will be very hard to maintain and update. 

Another option is you can host your site with different ports and then use the reverse proxy to map to map the domain name to the port





Explanation

Scenario 

I have two sites running on my vultr vps. All of them are website that means request will be come in with  domain name and going to be arrived on the port 80. If  I assing one program [ site ] to listen to the port 80 others will not be able to listen on port 80. 

Condition
Requirement is that requests should be listend on port 80 and handled by the app to which it relates.
After struggling for 3 days i got this 😂 silly solution.


Source Code ( solution I got )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
func main() {
 // New functionality written in Go
 http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "New function")
 })

 // gospot.mchampaneri.in ===> localhost:8085
 u1, _ := url.Parse("http://localhost:8085/")
 http.Handle("gospot.mchampaneri.in/", httputil.NewSingleHostReverseProxy(u1))

 // www.mchampaneri.in ===> localhost:8081 
 u2, _ := url.Parse("http://localhost:8081/")
 http.Handle("www.mchampaneri.in/", httputil.NewSingleHostReverseProxy(u2))

 // Start the server
 http.ListenAndServe(":80", nil)
}
Compiled binary of this code is proxy server for us


Explanation  Of Code

We are using http package and httputil package to make this done.
I assign my every sites port in serise of 80xx

Now Created another golang app which works as http mux .
[ Which's Source Code I am Showing. Ya It Just 17+ 😁(imports + package main) lines for me]

Explanation of the core code

u1, _ := url.Parse("http://localhost:8085/")
In this line we are parsing the url of our end point
[my app of gospot.mchampaneri.in is running on local 8085 port]

http.Handle("gospot.mchampaneri.in/", httputil.NewSingleHostReverseProxy(u1))
It matches the request with the .Handle( input-url ) and if matches it forwards the request to the url which we parsed before.


Explaination of Work

After seeing code if still you are confused how it works, let me explaing the flow.

  • You search for gospot.mchampaner.in in you browser
  • DNS server will forward request to my vultu vps on port 80
  • Our Proxy is running on port 80 it will receive request
  • Proxy will match the request with the http.Handle("gospot.mchampaneri.in/") and so request will be handled by httputil.NewSignelHostReverseProxy(u1) [ which is actully the reverse proxy]


So inshort this proxy will map incomming request domain to the port on your vps which is inteded to handle it.

Hope you will find it usefull and easy to understand.
For this post your comments are welcomed so i can make it better.

Comments

  1. Hey Manish,
    i want to monitor the request and response of few API in my project using exactly such proxy server, please help..lets get connect via email : bahalkapil@gmail.com

    ReplyDelete
  2. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... dr-wall

    ReplyDelete
  3. We can also assure ourselves that we can make safe transactions and manage our financial accounts online. We can also keep our Internet service provider or ISP from tracking our web history and recording our tracks. related site

    ReplyDelete
  4. In the huge web hosting industry, there are many type of hosting that are available for someone who is looking to build a website. Knowing how many types of hosting that is available to them would certainly help them in making their choice. Instead of overloading your mind with all the various types of hosting out there, you should start with the basics. In fact, there are two basic type of hosting that you can use. It is either a paid hosting or a free hosting service. It is not a must for you to spend in order to have a website. There are free options out there that you can use but you need to know the details about this type of hosting service before you decide. https://hostinglelo.in/

    ReplyDelete
  5. A VPN that can be used no matter what country you are in, all websites and social APPs in the world can be accessed at high speed without any problems. For details, click : Try PandaVPN anonymously

    ReplyDelete

Post a Comment

Popular posts from this blog

Google blogger Ideas panel

Google blogger Ideas  I opened by blogger today, and..   I got this.  Google blogger Ideas  A panel suggesting a topic on which I can write my next blog. It's fetching unanswered question from web according to your previous post and topics. It was something, I was really looking for, after all it takes time to finding subject on which to write next and still being in the same niche.  Awesome feature Blogger! 

Apache : setup basic auth with apache in windows

Authentication is any process by which you verify that someone is who they claim they are. Authorization is any process by which someone is allowed to be where they want to go or to have information that they want to have. I will show here how to set up basic auth on the apache with windows. Pre-requests  Windows VPS Apache server ( That's it ) ( In windows it might be difficult to setup the Apache alone. So instead use something ling xampp , wamp or laragon .) RestClient (  I personally use the postman , but you can use your preferable client)  Windows VPS provider Steps  Enable the necessary modules in the Apache Create the password file Set the auth directives in the virtual host file. Verify basic auth. Enable the  necessary   modules  in the Apache Open the httpd.conf file in the apache's conf folder. httpd.conf file Enable the necessary modules to make the basic auth working. Necessary modules  mod_auth_basic

Sciter : GUI Application with Golang using HTML/CSS

GUI library for golang sciter This is the words from Sciter's Web site, Sciter brings a stack of web technologies to desktop UI development. Web designers and developers can reuse their experience and expertise in creating modern looking desktop applications. Various GUI frameworks offer different UI declaration and styling languages, such as QML and  XAML (Microsoft WPF) . On the contrary, Sciter allows using time proven, robust, and flexible HTML and CSS for GUI definition and GPU accelerated rendering.   Before using sciter I already tried other alternatives but none of them was satisfactory as an example first i tried andlabs / ui  library   i already have written a post on it. You can read it on post gui programming with golang .  But this library is still under construction and has no support for production apps. Secondly, I go for electron but the problem was my simple calc like the app was of size 150mb. Which is 15mb of go and other was the e