drryyiuib43562604 2018-11-29 20:58
浏览 338
已采纳

自己的https certbot网络服务器返回我的404页面未找到

I have pretty simple go https web server

Here is code:

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.
"))
    // fmt.Fprintf(w, "This is an example server.
")
    // io.WriteString(w, "This is an example server.
")
}

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)
    }
}

It listens on port 8085 which is open by farewall. I use SSL certificate generated by certbot:

sudo certbot certonly --standalone -d example.com

So I built web server using following generated files:

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

Here is port state:

$ 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)

So when I try to from another machine:

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

Web server run on DigitalOcean. Do I need to configure my droplet somehow? Not sure what I missed? Also I have certificate.crt, certificate_ca.crt, certificate.csr files I got from company that sold me domain. Should I use this files someway? I need this for OAuth redirect URI.

  • 写回答

1条回答 默认 最新

  • dpub33855 2018-11-30 02:31
    关注

    You have configured a handler for the URL path /hello, but you haven't configured a handler for the path /. Thus when you try to load that path, you get a 404.

    If you tried to load https://example.com:8085/hello then you would see the example text.

    You could also set up a route for /, e.g.:

     http.HandleFunc("/", HelloServer)
    

    Note that because this matches all possible URLs, you need to check the URL explicitly if you wanted to match only the homepage, for instance:

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

    You should also consider using a more flexible multiplexer, such as gorilla/mux.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了