0

I have a http2 server but by default it responds http1 requests.

I want to stop my server responding to http1 requests?

Most browsers might use alpn or npn. is there a possibility to advertise only http2 ? or a custom list of application protocols ?

2 Answers2

2

In Caddy, if you're comfortable modifying the source code, you can make the following changes in caddyhttp/httpserver/server.go.

Change the line that says:

var defaultALPN = []string{"h2", "http/1.1"}

…so that it says:

var defaultALPN = []string{"h2"}

This will prevent it from advertising HTTP/1.1 via ALPN.

Then add this code to the beginning of the ServeHTTP method:

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if !r.ProtoAtLeast(2, 0) {
        if hj, ok := w.(http.Hijacker); ok {
            conn, _, err := hj.Hijack()
            if err == nil {
                conn.Close()
            }
        }
        return
    }
    …
}

This will immediately close the connection without sending headers if any protocol earlier than HTTP/2 is used.

0

You could use something like below rule to block all http1 requests. But it is not advisable to do this.

if ($server_protocol ~* "HTTP/1*") {
    return 444;
} 
tux
  • 1,450
  • 13
  • 19