go http server安全小技巧
package main
import (
"io"
"log"
"net/http"
)
func main() {
// Hello world, the web server
helloHandler := func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, world!\n")
}
http.HandleFunc("/hello", helloHandler)
log.Fatal(http.ListenAndServe(":80", nil))
}
这是一个简单的go http server例子
package main
import (
"io"
"log"
"net/http"
)
func main() {
// Hello world, the web server
helloHandler := func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, world!\n")
}
//这个可以防止直接ip访问和恶意绑定域名
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//重定向到首页
http.Redirect(w, r, "https://www.domain.com", http.StatusMovedPermanently)
})
//指定域名访问
http.HandleFunc("www.domain.com/hello", helloHandler)
http.HandleFunc("domain.com/hello", helloHandler)
log.Fatal(http.ListenAndServe(":80", nil))
}
上面加了一些代码是防止直接ip访问和防止恶意绑定其他域名