自己的 https certbot 网络服务器 returns 找不到我的 404 页面

Own https certbot web server returns me 404 page not found

我很简单go https web server

代码如下:

package main

import (
    // "fmt"
    // "io"
    "net/http"
    "log"
)

func HelloServer(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    w.Write([]byte("This is an example server.\n"))
    // fmt.Fprintf(w, "This is an example server.\n")
    // io.WriteString(w, "This is an example server.\n")
}

func main() {
    http.HandleFunc("/hello", HelloServer)
    err := http.ListenAndServeTLS(":8085", "fullchain.pem", "privkey.pem", nil)
//    err := http.ListenAndServeTLS(":8085", "certificate_ca.crt", "certificate.csr", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

它侦听由防火墙打开的端口 8085。 我使用 certbot 生成的 SSL 证书:

sudo certbot certonly --standalone -d example.com

所以我使用以下生成的文件构建了网络服务器:

err := http.ListenAndServeTLS(":8085", "fullchain.pem", "privkey.pem", nil)

这是端口状态:

$ sudo netstat -tulpn | grep 8085
tcp6       0      0 :::8085                 :::*                    LISTEN      23429/hello

$ sudo ufw status | grep 8085
8085/tcp                   ALLOW       Anywhere
8085/tcp (v6)              ALLOW       Anywhere (v6)

所以当我尝试从另一台机器上:

$ curl -sL https://example.com:8085
404 page not found

DigitalOcean 上的 Web 服务器 运行。我需要以某种方式配置我的 Droplet 吗?不确定我错过了什么? 我还有 certificate.crt, certificate_ca.crt, certificate.csr 个文件,这些文件是从卖给我域名的公司那里得到的。我应该以某种方式使用这些文件吗? 我需要这个用于 OAuth 重定向 URI。

您已为 URL 路径 /hello 配置了处理程序,但尚未为路径 / 配置处理程序。因此,当您尝试加载该路径时,您会收到 404.

如果您尝试加载 https://example.com:8085/hello 那么您会看到示例文本。

您还可以为 / 设置路线,例如:

 http.HandleFunc("/", HelloServer)

请注意,因为这会匹配所有可能的 URL,如果您只想匹配主页,则需要明确检查 URL,例如:

    if req.URL.Path != "/" {
        http.NotFound(w, req)
        return
    }

您还应该考虑使用更灵活的多路复用器,例如gorilla/mux。