【问题标题】:Own https certbot web server returns me 404 page not found自己的 https certbot 网络服务器返回 404 页面未找到
【发布时间】:2019-05-02 00:20:54
【问题描述】:

我有很简单的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 端口上侦听,该端口由 farewall 开放。 我使用 certbot 生成的 SSL 证书:

sudo certbot certonly --standalone -d example.com

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

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

Web 服务器在 DigitalOcean 上运行。我需要以某种方式配置我的液滴吗?不确定我错过了什么? 我也有从向我出售域名的公司获得的certificate.crt, certificate_ca.crt, certificate.csr 文件。我应该以某种方式使用这些文件吗? 我需要这个用于 OAuth 重定向 URI。

【问题讨论】:

    标签: ssl go https digital-ocean certbot


    【解决方案1】:

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

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

    您也可以为/ 设置路由,例如:

     http.HandleFunc("/", HelloServer)
    

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

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

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

    【讨论】:

    • 答案在表面上!非常感谢!
    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2014-11-01
    • 1970-01-01
    • 2020-04-17
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多