如何使用虚拟主机在GO中使用Virtual Hosts功能性服务器静态文件
如何使用GO中虚拟主机的服务器静态文件(使用fileserver
)?
如果我有自己的处理程序功能,那么该任务似乎很容易解决[ 1 < /a>]:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
})
http.HandleFunc("qa.example.com/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, improved world!")
})
http.ListenAndServe(":8080", nil)
}
但是,如果我需要使用静态文件(fileserver
)该怎么办 对于虚拟主机?
这
r.PathPrefix("qa.example.com/").Handler(http.FileServer(http.Dir("/static/qa/")))
是行不通的 - 只是被忽略了。
我在做什么错? 这种方法通常是错误的吗?
How can I server static files (with FileServer
) for a virtual host in Go?
If I have my own handler function, the task seems to be easily solvable [1]:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
})
http.HandleFunc("qa.example.com/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, improved world!")
})
http.ListenAndServe(":8080", nil)
}
But what if I need to serve static files (with FileServer
)
for a virtual host?
This
r.PathPrefix("qa.example.com/").Handler(http.FileServer(http.Dir("/static/qa/")))
does not work — it is just ignored.
What am I doing wrong?
Is this approach generally wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行test Exec命令
curl -h“主机:my.example.com” 127.0.0.1:8080/static/01.go
,01.go
重新静态文件名。run test exec command
curl -H "Host: my.example.com" 127.0.0.1:8080/static/01.go
,01.go
replacte your static filename.注册
主机/路径
的处理程序。仅在调用文件处理程序时才剥离/path
零件。此注册为
QA.example.com/static/*
提供的文件提供了文件./ static/QA/
。Register a handler for
host/path
. Strip the/path
part only when invoking the file handler.This registration serves files for
qa.example.com/static/*
from the directory./static/qa/
.