Reputation: 385
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...
Upvotes: 3
Views: 5505
Reputation: 133
I use both but Prefer second solution the most :
nginx as proxy having multiple Golang app on multiple port i.e. GoApp1:8081 GoApp2:8082 GoApp3:8083 ....
server { ... server_name www.example1.com example1.com; ... location / { proxy_pass app_ip:8081; } ... } server { ... server_name www.example2.com example2.com; ... location / { proxy_pass app_ip:8082; } ... } . . .
nginx as proxy having multiple Golang app on same port(8081) separating by(Golang) func (u *URL) Hostname() string
server { ... server_name www.example1.com example1.com; ... location / { proxy_pass app_ip:8081; } ... }
There is another way around(without nginx): sudo apt-get install iptables-persistent
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8081
sudo netfilter-persistent save
sudo netfilter-persistent reload
Upvotes: 1
Reputation: 1817
You don't really need anything in front, like nginx, although in some cases it is recommended.
Please find similar answer here "Host Multiple Golang Sites on One IP and Serve Depending on Domain Request"
Upvotes: 2
Reputation: 1389
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;
}
}
Upvotes: 8