在Linux中,静态文件服务返回404错误的问题

huangapple go评论82阅读模式
英文:

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(&quot;/upload&quot;, uploadFile).Methods(&quot;POST&quot;)
    
    //Test Message
    r.HandleFunc(&quot;/test&quot;, test).Methods(&quot;GET&quot;)

    r.PathPrefix(&quot;/site/&quot;).Handler(http.StripPrefix(&quot;/site/&quot;, http.FileServer(http.Dir(&quot;site&quot;))))


if err := http.ListenAndServe(&quot;:8010&quot;, r); err != nil {
		log.Fatal(&quot;ListenAndServe: &quot;, 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, &quot;Welcome it Works!&quot;)
}

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/
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot; /&gt;
    &lt;title&gt;gAnalytics&lt;/title&gt;
   
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;script defer src=&quot;./dist/appXYZ.min.js&quot;&gt;&lt;/script&gt;&lt;/head&gt;
&lt;/html&gt;

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
    │&#160;&#160; ├── 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.

huangapple
  • 本文由 发表于 2021年7月7日 13:26:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/68280504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定