http2

Enables HTTP/2 support in the specified context of NGINX configuration.

Syntaxhttp2;
Defaultoff
Contexthttp, server
Argumentsflag

Description

The 'http2' directive enables the HTTP/2 protocol for incoming requests handled by NGINX. This directive can be specified in the 'http' or 'server' contexts, and it is particularly useful when used with the 'listen' directive to indicate that a server block should support HTTP/2. When enabled, HTTP/2 protocol features such as multiplexing, header compression, and prioritized requests will be available, enhancing the performance and efficiency of served content.

The 'http2' directive accepts a flag as its argument, which specifies whether HTTP/2 is enabled (on) or disabled (off). By default, this directive is set to 'off', meaning HTTP/2 support is disabled unless explicitly enabled in the server or http block. When enabled, clients capable of HTTP/2 can negotiate the usage of this protocol when making requests to the server, allowing for better resource utilization and improved loading speeds for web pages served by NGINX.

It’s important to note that to use HTTP/2, the server must be configured to use TLS/SSL, as most browsers require an encrypted connection to allow HTTP/2. Thus, it is common to see this directive used alongside SSL-related directives in the server block for secure connections.

Config Example

server {
    listen 443 ssl http2;
    server_name example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
}

Ensure that SSL is enabled for the server, as most browsers require HTTPS for HTTP/2.

Avoid using HTTP/2 on server blocks that only require HTTP/1.1, as this may cause unnecessary complications in negotiations.

Make sure that your upstream servers and proxies also support HTTP/2 if feature interoperability is crucial.

← Back to all directives