Ahmadreza Zibaei Ahmadreza Zibaei - 1 month ago 15
HTTP Question

Is it possible to handle multi domains in Golang?

I want to handle multi-domains with Golang.

Like this , If browser sends request for http://sampledomain.me handles it with specific Golang program ,

In other cases , I want many programs on one server and tell each one of them , to handle each domain.

All programs are listening on 80 and many domains point to one server.

Can anybody help me ?

Thx...

Answer

Your Go programs can't all listen on port 80. You'd need something in front, like nginx, to act as a reverse proxy. The following nginx config excerpt would forward two domains on to two different programs, each listening on different ports:

server {
        listen 80;
        server_name www.domain1.com;

        location / {
                proxy_pass http://127.0.0.1:8080;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host            $host;
                proxy_read_timeout 600s;
        }

}  

server {
        listen 80;
        server_name www.domain2.com;

        location / {
                proxy_pass http://127.0.0.1:8081;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host            $host;
                proxy_read_timeout 600s;
        }

}