英文:
Go Static Files Serve giving 404 in Linux
问题
我有一个如下所示的Go语言简单Web服务器:
func webServerWay() {
r := mux.NewRouter()
//文件上传代码
r.HandleFunc("/upload", uploadFile).Methods("POST")
//测试消息
r.HandleFunc("/test", test).Methods("GET")
r.PathPrefix("/site/").Handler(http.StripPrefix("/site/", http.FileServer(http.Dir("site"))))
if err := http.ListenAndServe(":8010", r); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
简单的测试函数如下所示:这是一个简单的函数,用于检查端点是否可达。
func test(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome it Works!")
}
网站结构如下:
├── go.mod
├── go.sum
├── main.go
└── site
├── dist
└── index.html
该应用在我的本地开发环境(Mac OS)中完美运行:
Ajay@apples-MacBook-Air:/$ curl localhost:8010/test
Welcome it Works!
Ajay@apples-MacBook-Air:/$ curl localhost:8010/site/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>gAnalytics</title>
</head>
<body>
<div id="root"></div>
</body>
<script defer src="./dist/appXYZ.min.js"></script></head>
</html>
但是,当我为Linux(Ubuntu 20.04 x64)创建构建时:
GOOS=linux GOARCH=386 go build -o gApp24v1.linux main.go
并在Linux中运行代码:
sudo ./gApp24v1.linux
静态站点服务不起作用,但我可以访问测试端点:
root@gapp24:/home/ajay/app# curl localhost:8010/test
Welcome it Works!
root@gapp24:/home/ajay/app# curl localhost:8010/site/
404 page not found
我尝试了多种处理静态文件的代码方式,查看了其他答案并阅读了文档,但它在我的macOS上运行正常,而在我的Linux机器上却不行。
有什么建议吗?
此外,我Linux(Ubuntu机器)上的netstat输出如下:
root@gapp24:/home/ajay/app# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 671/vsftpd
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 547/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 704/sshd: /usr/sbin
tcp6 0 0 :::8010 :::* LISTEN 1167/./gApp24v1.lin
tcp6 0 0 :::22 :::* LISTEN 704/sshd: /usr/sbin
英文:
I have a Simple Web Server in Go as below:
func webServerWay() {
r := mux.NewRouter()
//File Upload Code
r.HandleFunc("/upload", uploadFile).Methods("POST")
//Test Message
r.HandleFunc("/test", test).Methods("GET")
r.PathPrefix("/site/").Handler(http.StripPrefix("/site/", http.FileServer(http.Dir("site"))))
if err := http.ListenAndServe(":8010", r); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Simple Test Function looks as below: It's A Simple Function to see if the endpoint is reachable.
func test(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome it Works!")
}
Site Structure is:
├── go.mod
├── go.sum
├── main.go
└── site
├── dist
└── index.html
The Application Works Perfectly in my local dev environment i.e. Mac OS:
Ajay@apples-MacBook-Air:/$ curl localhost:8010/test
Welcome it Works!
Ajay@apples-MacBook-Air:/$ curl localhost:8010/site/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>gAnalytics</title>
</head>
<body>
<div id="root"></div>
</body>
<script defer src="./dist/appXYZ.min.js"></script></head>
</html>
But When I Create a Build for Linux(Ubuntu 20.04)x64
GOOS=linux GOARCH=386 go build -o gApp24v1.linux main.go
And run the code in Linux:
sudo ./gApp24v1.linux
The Static Site Serving Doesn't Work, But I can reach the Test Endpoint:
root@gapp24:/home/ajay/app# curl localhost:8010/test
Welcome it Works!
root@gapp24:/home/ajay/app# curl localhost:8010/site/
404 page not found
I have tried multiple ways of code in static file handler seeing other answers and reading the document but it works in my macOS and not my Linux Machine.
Any Suggestions!
Also the netstat in my Linux(Ubuntu machine) is below:
root@gapp24:/home/ajay/app# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 671/vsftpd
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 547/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 704/sshd: /usr/sbin
tcp6 0 0 :::8010 :::* LISTEN 1167/./gApp24v1.lin
tcp6 0 0 :::22 :::* LISTEN 704/sshd: /usr/sbin
答案1
得分: 0
在部署构建文件时,还需要相应地移动静态文件:
这里的gApp24v1.linux
是Linux的构建文件。包含文件夹site
的静态文件也需要放置在相应的路径下:
.
├── gApp24v1.linux
└── site
├── dist
│ ├── appXYZ.min.js
└── index.html
英文:
When Deploying the build file, the respective static files need to be also moved accordingly:
Here gApp24v1.linux
is the build for Linux. The static files containing folder site
needs to be also placed in the respective path:
.
├── gApp24v1.linux
└── site
├── dist
│   ├── appXYZ.min.js
└── index.html
答案2
得分: 0
你可以使用这个仓库https://github.com/go-bindata/go-bindata
,将静态文件压缩成可管理的Go源代码,而不是移动静态站点目录。
英文:
Instead of moving Static Site directory, you can use this repo https://github.com/go-bindata/go-bindata
to zip your static file into manageable Go source code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论