# Gin HTTP/2 and HTTP/3 (QUIC) Support: Implementation Guide

> Learn how Gin supports HTTP/2 and HTTP/3 (QUIC). Implement faster web protocols automatically with TLS, h2c flags, and the RunQUIC method. Boost your Go web app performance today.

- Repository: [Gin-Gonic/gin](https://github.com/gin-gonic/gin)
- Tags: how-to-guide
- Published: 2026-02-27

---

**Gin enables HTTP/2 over TLS automatically, clear-text HTTP/2 (h2c) via the `UseH2C` configuration flag, and HTTP/3 (QUIC) through the dedicated `RunQUIC` method, leveraging Go's standard `net/http` and the `quic-go` library.**

The `gin-gonic/gin` framework extends Go's standard HTTP server to support modern protocol versions with minimal configuration. Understanding how Gin implements HTTP/2 and HTTP/3 (QUIC) support helps developers optimize performance for high-concurrency applications. This guide examines the source code implementation in [`gin.go`](https://github.com/gin-gonic/gin/blob/main/gin.go) to explain the three distinct pathways for enabling these protocols.

## HTTP/2 Support Mechanisms

### Automatic HTTP/2 Over TLS

When you invoke `Engine.RunTLS`, Gin relies on Go's standard `http.Server` to automatically negotiate HTTP/2 as defined in RFC 7540. No additional configuration is required because the underlying server enables HTTP/2 when TLS is present.

### Clear-Text HTTP/2 (h2c) Configuration

For environments where TLS termination occurs at a load balancer or proxy, Gin supports HTTP/2 without encryption through the **h2c** (HTTP/2 Cleartext) protocol.

In [`gin.go`](https://github.com/gin-gonic/gin/blob/main/gin.go), the `Engine` struct exposes a boolean flag `UseH2C` at lines 169-170. When set to `true`, the `Engine.Handler()` method wraps the router with `h2c.NewHandler`, injecting an `http2.Server` instance that understands HTTP/2 frames on plain TCP connections.

The implementation creates the HTTP/2 server lazily at lines 44-48:

```go
h2s := &http2.Server{}
h2cHandler := h2c.NewHandler(engine.Handler(), h2s)

```

This allows clients to communicate via HTTP/2 without TLS overhead between the proxy and the Gin application.

## HTTP/3 (QUIC) Implementation

### The RunQUIC Method

Gin provides native HTTP/3 support through integration with the `quic-go` library. The framework imports `github.com/quic-go/quic-go/http3` and exposes the `RunQUIC(addr, certFile, keyFile string)` method.

Located at lines 27-41 in [`gin.go`](https://github.com/gin-gonic/gin/blob/main/gin.go), this method constructs a TLS-configured QUIC listener and passes Gin's `Handler()` to `http3.ListenAndServeQUIC`. The call blocks the executing goroutine and returns any startup errors, providing a single-line solution for UDP-based HTTP/3 serving.

## Configuration Examples

Practical implementation requires specific setup for each protocol mode:

```go
package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("/", func(c *gin.Context) {
		c.String(200, "Protocol negotiation successful")
	})

	// HTTP/2 over TLS (automatic)
	// r.RunTLS(":443", "server.crt", "server.key")

	// HTTP/2 cleartext (h2c)
	r.UseH2C = true
	r.Run(":8080")

	// HTTP/3 (QUIC)
	// r.RunQUIC(":8443", "server.crt", "server.key")
}

```

For h2c support, you must set `engine.UseH2C = true` before calling `Run` or `RunListener`. For HTTP/3, valid TLS certificates are mandatory since QUIC requires encryption.

## Summary

- **HTTP/2 over TLS** activates automatically when using `Engine.RunTLS` through Go's standard library
- **h2c support** requires setting `Engine.UseH2C = true` to enable clear-text HTTP/2 behind TLS-terminating proxies
- **HTTP/3 (QUIC)** is available via `Engine.RunQUIC`, wrapping the `quic-go` HTTP/3 server implementation
- All implementations reside in [`gin.go`](https://github.com/gin-gonic/gin/blob/main/gin.go), with the `Handler()` method serving as the integration point for protocol wrappers

## Frequently Asked Questions

### Does Gin require manual configuration for HTTP/2 with TLS?

No. When using `RunTLS`, Go's `http.Server` automatically negotiates HTTP/2 according to RFC 7540. Gin inherits this behavior automatically without requiring explicit HTTP/2 configuration.

### What is the purpose of the UseH2C flag in Gin?

The `UseH2C` boolean flag enables HTTP/2 over cleartext TCP connections. This is essential for deployments behind load balancers that terminate TLS, allowing the internal traffic to use HTTP/2 multiplexing without double encryption.

### Which dependencies provide HTTP/3 support in Gin?

Gin imports `github.com/quic-go/quic-go/http3` to provide HTTP/3 capabilities. The `RunQUIC` method in [`gin.go`](https://github.com/gin-gonic/gin/blob/main/gin.go) serves as a thin wrapper around this library's `ListenAndServeQUIC` function.

### Can Gin serve HTTP/2 and HTTP/3 simultaneously?

While `RunQUIC` starts an HTTP/3 server independently, you would need separate goroutines or listeners to serve HTTP/2 and HTTP/3 on different ports simultaneously, as each method blocks its calling goroutine.