Linux 中静态文件服务返回404错误
时间:2024-02-13
浏览:293
作者:佚名
golang学习网今天将给大家带来《Linux 中静态文件服务返回404错误》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
问题内容
我有一个简单的 go web 服务器,如下所示:
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)
}
}
简单测试函数如下所示:这是一个简单函数,用于查看端点是否可达。
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
正确答案
部署构建文件时,相应的静态文件也需要移动:
这里 gapp24v1.linux 是针对 linux 的构建。包含文件夹 site 的静态文件也需要放置在相应的路径中:
.
├── gApp24v1.linux
└── site
├── dist
│ ├── appXYZ.min.js
└── index.html您可以使用此存储库 https://github.com/go-bindata/go-bindata 将静态文件压缩为可管理的 go 源代码,而不是移动静态站点目录。
理论要掌握,实操不能落!以上关于《Linux 中静态文件服务返回404错误》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
站内热门
最新文章
12.01
12.01
热门专题
