本地主机默认情况下未启用HTTP2。

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

HTTP2 not enabled by default on localhost

问题

我可能对此一无所知,但是我的基本本地服务器由于某种奇怪的原因没有启用HTTP2。通常我会在Caddy后面使用代理,但是由于我不想在这个项目中使用我的域名,所以我创建了一个基本的Go服务器并运行它,它工作得很好,但是头部显示的是HTTP/1.1而不是2.0,出了什么问题?

package main

import (
  "fmt"
  "net/http"
  "html/template"
  "os"
)

func IfError(err error, Quit bool) {
  if err != nil {
    fmt.Println(err.Error())
    if(Quit) {
      os.Exit(1);
    }
  }
}

func ServeHome(w http.ResponseWriter, r *http.Request) {
  t, err := template.ParseFiles("html/home")
  IfError(err, false)
  err = t.Execute(w, nil)
  IfError(err, false)
}

func RedirectRoot(fs http.Handler, home http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
      home.ServeHTTP(w, r)
    } else {
      fs.ServeHTTP(w, r)
    }
  })
}

func main()  {
  proto := ":8081"
  ServeFiles := http.FileServer(http.Dir("static/"))
  http.Handle("/", RedirectRoot(ServeFiles, http.HandlerFunc(ServeHome)))
  fmt.Printf("Listening on ... %s", proto)
  IfError(http.ListenAndServe(proto, nil), true)
}

非常基础的东西,但是即使文档说默认情况下它可以工作,但实际上并不起作用。另外,我的Go版本是1.8.3。

英文:

I might be just clueless on this but my basic localhost server doesn't have HTTP2 Enabled for some odd reason, I normally proxy behind Caddy, but as I don't want to use my domain for this side project, I created a basic server in Go, and ran it, it works okay, but the headers show HTTP/1.1 instead of 2.0, what's wrong?

package main

import (
  "fmt"
  "net/http"
  "html/template"
  "os"
)

func IfError(err error, Quit bool) {
  if err != nil {
    fmt.Println(err.Error())
    if(Quit) {
      os.Exit(1);
    }
  }
}

func ServeHome(w http.ResponseWriter, r *http.Request) {
  t, err := template.ParseFiles("html/home")
  IfError(err, false)
  err = t.Execute(w, nil)
  IfError(err, false)
}

func RedirectRoot(fs http.Handler, home http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
      home.ServeHTTP(w, r)
    } else {
      fs.ServeHTTP(w, r)
    }
  })
}

func main()  {
  proto := ":8081"
  ServeFiles := http.FileServer(http.Dir("static/"))
  http.Handle("/", RedirectRoot(ServeFiles, http.HandlerFunc(ServeHome)))
  fmt.Printf("Listening on ... %s", proto)
  IfError(http.ListenAndServe(proto, nil), true)
}

Very basic stuff, but doesn't work even thought the documentation says it works by default. Also, my go version is 1.8.3

答案1

得分: 2

是的,当你使用SSL证书时,默认情况下启用了HTTP/2协议支持。

err := http.ListenAndServeTLS(":8081", "server.crt", "server.key", handler)
if err != nil && err != http.ErrServerClosed {
    log.Fatal("ListenAndServe: ", err)
}

然后,通过以下方式访问:

https://localhost:8081/

文档参考

英文:

Yes, its enabled by default when you use with SSL certs.

> Doc Reference: Starting with Go 1.6, the http package has transparent
> support for the HTTP/2 protocol when using HTTPS.

err := http.ListenAndServeTLS(":8081", "server.crt", "server.key", handler)
if err != nil && err != http.ErrServerClosed {
    log.Fatal("ListenAndServe: ", err)
}

Then, access it via

https://localhost:8081/

huangapple
  • 本文由 发表于 2017年7月2日 08:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/44866396.html
匿名

发表评论

匿名网友

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

确定