Skip to main content

Configuring HTTP/2 Protocol

1. Enabling HTTP/2 Module

Directive Values Description
LoadModule http2_module modules/mod_http2.so Load the HTTP/2 module into Apache
Protocols h2 http/1.1 Enable HTTP/2 and fallback to HTTP/1.1

Example: Enabling HTTP/2

<IfModule http2_module>
    Protocols h2 http/1.1
</IfModule>
Note: Ensure mod_http2 is installed and enabled in your Apache build.

2. Configuring HTTP/2 Protocol

Directive Description Values Notes
Protocols Enables or disables HTTP/2 and other protocols http/1.1, h2, h2c Use h2 to enable HTTP/2 over TLS, h2c for cleartext HTTP/2
ProtocolsH2 Enables HTTP/2 over TLS only on, off Deprecated in favor of Protocols
H2MaxWindowSize Maximum window size for HTTP/2 streams integer Default is 65535 bytes
H2MaxConcurrentStreams Maximum number of concurrent streams per connection integer Default is 100

Example: Enabling HTTP/2 with TLS

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    Protocols h2 http/1.1
</VirtualHost>

Example: Enabling HTTP/2 with ALPN

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    Protocols h2 http/1.1
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
</VirtualHost>

3. Using HTTP/2 over TLS

Directive Description Default Notes
Protocols Enables or disables specific protocols including HTTP/2 http/1.1 Must include h2 to enable HTTP/2
ProtocolsH2 Enables HTTP/2 only (disables HTTP/1.1) Off Requires TLS to be enabled
H2Direct Enables HTTP/2 direct (without ALPN) Off Not recommended for production use

Example: Enabling HTTP/2 with TLS

<VirtualHost *:443>
    ServerName example.com
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    Protocols h2 http/1.1
</VirtualHost>
Note: HTTP/2 requires TLS 1.2 or higher and is not supported over plain HTTP.
Warning: Ensure that your SSL/TLS configuration supports HTTP/2. Older cipher suites may not be compatible.

4. Configuring HTTP/2 Cleartext

Directive Description Default Notes
Protocols Enables or disables specific protocols including HTTP/2 http/1.1 Must include h2 to enable HTTP/2
ProtocolsH2 Enables HTTP/2 protocol only Disabled Available in Apache 2.4.23+

Example: Enabling HTTP/2 Cleartext

<VirtualHost *:80>
    ServerName example.com
    Protocols h2 http/1.1
    DocumentRoot /var/www/html
</VirtualHost>
Note: HTTP/2 over cleartext (port 80) requires the Protocols directive and is not enabled by default.
Warning: HTTP/2 over cleartext is not secure and should only be used in development or internal networks.

5. Setting Stream Window Size

Directive Values Description
Http2StreamWindowSize 65536 to 2147483647 Defines the initial window size (in bytes) for HTTP/2 streams. Default is 65536 bytes.

Example: Setting Stream Window Size

<IfModule mod_http2.c>
    Http2StreamWindowSize 1048576
</IfModule>
Note: Increasing the stream window size can improve performance for large file transfers but may increase memory usage.

6. Configuring Server Push

Directive Description Values Notes
Protocols Enables HTTP/2 protocol support h2, h2c Must be enabled to use server push
Push Enables or disables server push on, off Default is off
PushProvider Specifies the provider for server push default, none Default is default

Example: Enabling Server Push

<VirtualHost *:443>
    ServerName example.com
    Protocols h2 h2c
    Push on
    PushProvider default
</VirtualHost>
Note: Server push is an HTTP/2 feature that allows the server to send resources to the client before they are explicitly requested, potentially improving page load times.

7. Setting Max Concurrent Streams

Directive Values Description
H2MaxConcurrentStreams 100 Maximum number of concurrent streams per connection for HTTP/2

Example: Configuring Max Concurrent Streams

<IfModule mod_http2.c>
  H2MaxConcurrentStreams 100
</IfModule>

8. Configuring HTTP/2 Timeout

Directive Values Description
H2MaxWorkerIdleSeconds 60 Maximum idle time for an HTTP/2 worker thread before it is terminated. Default is 60 seconds.
H2MaxWorkers 100 Maximum number of HTTP/2 worker threads to use. Default is 100.
H2MinWorkers 5 Minimum number of HTTP/2 worker threads to keep active. Default is 5.
H2MaxStreamsWithoutData 1000 Maximum number of streams that can be open without sending data. Default is 1000.

Example: Setting HTTP/2 Worker Limits

H2MaxWorkers 150
H2MinWorkers 10
H2MaxWorkerIdleSeconds 90
Note: These directives are part of the mod_http2 module and require Apache 2.4.35 or later.

9. Setting Priority Handling

Directive Description Default Notes
HTTP2MaxReservedStreams Maximum number of reserved streams per connection 100 Controls how many streams can be reserved at once
HTTP2StreamMaxAge Maximum age of a stream in seconds before it is closed 300 Helps manage resource usage on long-lived connections
HTTP2MaxConcurrentStreams Maximum number of concurrent streams per connection 100 Enforces limits on concurrent stream handling

Example: Configuring HTTP/2 Stream Limits

<IfModule mod_http2.c>
    HTTP2MaxReservedStreams 50
    HTTP2StreamMaxAge 600
    HTTP2MaxConcurrentStreams 150
</IfModule>
Note: These directives are only available when mod_http2 is enabled and configured.

10. Using Early Hints

Directive Description Values Notes
HTTP2EarlyHint Enables or disables Early Hints support on, off Available in Apache 2.4.52+

Example: Enabling Early Hints

HTTP2EarlyHint on
Note: Early Hints are part of HTTP/2 and require a compatible client to benefit from the performance improvements.

11. Testing HTTP/2 Support

Example: Check HTTP/2 support with curl

curl -I --http2 https://example.com
Flag Description Notes
--http2 Enables HTTP/2 for the request Requires curl built with nghttp2
--http2-prior-knowledge Assumes HTTP/2 support without negotiation Useful for known HTTP/2 servers
Note: Ensure the server is configured to support HTTP/2 via Protocols directive in Apache.
Warning: HTTP/2 requires TLS (HTTPS) to function properly.

12. Troubleshooting HTTP/2 Issues

Issue Possible Cause Solution
No HTTP/2 support Module not loaded or OpenSSL version too old Ensure mod_http2 is loaded and OpenSSL >= 1.0.2
Slow response times Server configuration not optimized for HTTP/2 Adjust MaxConnectionsPerChild, KeepAliveTimeout
Mixed content errors HTTP/2 with HTTP 1.1 resources Ensure all assets are served over HTTP/2 or use Upgrade-Insecure-Requests
Connection resets Client or server timeout settings too low Increase Timeout and KeepAliveTimeout

Example: Enabling HTTP/2 with mod_http2

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    Protocols h2 http/1.1
    H2MaxSessionMem 65536
</VirtualHost>
Note: HTTP/2 requires TLS (HTTPS) to function properly.
Warning: Some older browsers do not support HTTP/2. Ensure fallbacks are in place.

Common HTTP/2 Troubleshooting Steps

  • Verify mod_http2 is installed and enabled
  • Check that SSL/TLS is properly configured
  • Review server logs for HTTP/2 handshake errors
  • Validate that client browsers support HTTP/2
  • Monitor performance metrics to identify bottlenecks

Was this article helpful?