Golang如何在单台物理服务器或VPS上运行多个Web服务器

Golang如何在单台物理服务器或VPS上运行多个Web服务器 大家好。

我有三个问题。

如何在一台物理服务器或VPS上运行多个Golang Web服务器?(一个运行在192.168.100.150,另一个运行在192.168.100.151)

如何在它们上面设置域名?

我们能否将它们都设置并运行在80端口?

6 回复

非常感谢。

请把代码也写在这里。 我只是个初学者。

更多关于Golang如何在单台物理服务器或VPS上运行多个Web服务器的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


请不要在公共环境中部署服务器,如果你无法回答自己提出的问题,或者在查阅相关文档后仍无法编写基础代码。

最终你可能会陷入僵尸网络或被他人接管……

嗯,我虽然没有实际尝试过,但 ListenAndServe 能够处理完整的地址(包括 IP 或域名以及端口)。我想你可以在两个不同的 IP 地址上使用两次(一次在 Go 协程中)或在不同的应用程序中使用相同的端口。此外,你还需要在接口上配置两个 IP 地址。

一个简单的服务器监听两个IP和相同端口的示例如下:

package main

import (
	"net/http"
)

func main() {
	http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello world!"))
	})
	go func() {
		http.ListenAndServe("192.168.100.150:8080", nil)
	}()
	http.ListenAndServe("192.168.100.151:8080", nil)
}

在这个简单示例中,我为两个服务器使用了相同的处理程序。如果你想为每个服务器使用独立的处理函数,建议使用gorilla mux

你好

你不需要使用 gorilla mux(至少在这种情况下不需要)。因为标准的 http 包可以创建多个多路复用器

package main

import (
	"io"
	"net/http"
)

func index1(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Hello from server 1")
}

func index2(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Hello from server 2")
}

func main() {

	mux1 := http.NewServeMux()
	mux1.HandleFunc("/", index1)

	mux2 := http.NewServeMux()
	mux2.HandleFunc("/", index2)

	go http.ListenAndServe("localhost:8001", mux1)

	http.ListenAndServe("localhost:8002", mux2)
}

在单台物理服务器或VPS上运行多个Golang Web服务器,可以通过以下几种方式实现:

方法1:使用不同端口

最简单的方式是让每个服务器监听不同的端口:

// server1.go - 监听8080端口
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Server 1 responding from 192.168.100.150:8080")
    })
    http.ListenAndServe("192.168.100.150:8080", nil)
}

// server2.go - 监听8081端口
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Server 2 responding from 192.168.100.151:8081")
    })
    http.ListenAndServe("192.168.100.151:8081", nil)
}

方法2:使用反向代理(都使用80端口)

通过Nginx反向代理实现多个域名共享80端口:

// app1.go - 应用1
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome to domain1.com - Server 1")
    })
    http.ListenAndServe(":8080", nil)
}

// app2.go - 应用2
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome to domain2.com - Server 2")
    })
    http.ListenAndServe(":8081", nil)
}

Nginx配置示例:

server {
    listen 80;
    server_name domain1.com;
    
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

server {
    listen 80;
    server_name domain2.com;
    
    location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

方法3:使用Go的虚拟主机(单端口多域名)

在单个Go程序中处理多个域名:

package main

import (
    "fmt"
    "net/http"
    "strings"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        host := strings.Split(r.Host, ":")[0] // 获取域名部分
        
        switch host {
        case "domain1.com":
            fmt.Fprintf(w, "Domain1.com - Server 1 content")
        case "domain2.com":
            fmt.Fprintf(w, "Domain2.com - Server 2 content")
        default:
            fmt.Fprintf(w, "Default server response")
        }
    })
    
    http.ListenAndServe(":80", nil)
}

方法4:使用不同的IP地址

如果服务器有多个IP,可以直接绑定到不同IP:

// 绑定到192.168.100.150:80
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Server on 192.168.100.150")
    })
    http.ListenAndServe("192.168.100.150:80", nil)
}

// 绑定到192.168.100.151:80
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Server on 192.168.100.151")
    })
    http.ListenAndServe("192.168.100.151:80", nil)
}

域名设置

域名设置需要在DNS管理界面添加A记录:

对于都使用80端口的需求,推荐使用方法2(反向代理)或方法3(虚拟主机),这样可以在单个80端口上服务多个域名。如果服务器有多个公网IP,也可以使用方法4直接绑定不同IP到80端口。

回到顶部