Web Golang路线不起作用
我刚开始进入golang,由于我计划至少托管两个网站,我选择使用Mux通过“过滤”域来显示不同的路由。每当我试图访问我的主要路线,它只会给我一个404错误。(另外,“www”部分不存在的事实是完全正常的。我不会键入它来访问该站点) 但是如果我以文件服务器的形式启动服务器,我就可以访问我的文件,所以我想服务器本身就是在工作的Web Golang路线不起作用,web,go,Web,Go,我刚开始进入golang,由于我计划至少托管两个网站,我选择使用Mux通过“过滤”域来显示不同的路由。每当我试图访问我的主要路线,它只会给我一个404错误。(另外,“www”部分不存在的事实是完全正常的。我不会键入它来访问该站点) 但是如果我以文件服务器的形式启动服务器,我就可以访问我的文件,所以我想服务器本身就是在工作的 func redirect(w http.ResponseWriter, req *http.Request) { target := "https://" + re
func redirect(w http.ResponseWriter, req *http.Request) {
target := "https://" + req.Host + req.URL.Path
http.Redirect(w, req, target,
http.StatusTemporaryRedirect)
}
func main() {
go http.ListenAndServe(":80", http.HandlerFunc(redirect)) // Redirection
// Serveur sécurisé
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("/root/go/src/web/static/"))))
s := r.Host("echecderi.me").Subrouter()
s.HandleFunc("/", indexEchec)
http.ListenAndServeTLS(":443", "domain-crt.pem", "domain-key.pem", nil)
}
func indexEchec(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Echec de rime</h1> </br> <img src=\"/static/echecderime/echec.gif\">")
}
func重定向(w http.ResponseWriter,req*http.Request){
目标:=“https://”+req.Host+req.URL.Path
http.重定向(w、req、目标、,
http.StatusTemporaryRedirect)
}
func main(){
转到http.ListenAndServe(“:80”,http.HandlerFunc(重定向))//重定向
//sécurisé服务员
r:=mux.NewRouter()
r、 PathPrefix(“/static/”)处理程序(http.StripPrefix(“/static/”),http.FileServer(http.Dir(“/root/go/src/web/static/”)))
s:=r.Host(“echecderi.me”).Subrouter()
s、 扶手板(“/”,索引)
http.listendservetls(“:443”,“domain crt.pem”,“domain key.pem”,nil)
}
func indexEchec(带http.ResponseWriter,r*http.Request){
fmt.Fprintf(w,“犯罪电子计算机”)
}
我认为您需要将r
作为http.listend和servetls
的最后一个参数,您还可以使用http.server实例
//create server instance
server := http.Server{
Addr: ":443",
TLSConfig: tlsConfig(cert),
}
rtr := mux.NewRouter()
rtr.HandleFunc("/profile", HandlerProfile).Methods("GET")
//rtr.HandleFunc( other routes...
//pass mux handler to server
server.Handler = rtr
server.ListenAndServeTLS("", "")
我的上帝,谢谢你。在golang文档中,他们使用了
nil
作为示例,因此我认为我需要使用它。但是r
到底意味着什么呢?最后一个参数是处理程序,它为请求提供服务r
是一种路由器,它接收请求并根据您设置请求的方式确定如何处理请求。如果将nil处理程序传递给ListenAndServeTLS
,它将使用默认处理程序(DefaultServeMux
)。有关更多信息,请参阅。