Nginx is a high-performance web server that also functions as a reverse proxy, load balancer, and HTTP cache. It is widely used for its ability to handle a large number of concurrent connections, making it ideal for serving static content and acting as a gateway for dynamic content. One of its modern features is support for the HTTP/2 protocol, which enhances web performance through multiplexing, header compression, and server push capabilities.
When Nginx is not serving content over HTTP/2, users may notice slower page load times or lack of expected performance improvements. This issue is typically observed when inspecting network requests in browser developer tools, where the protocol used is shown as HTTP/1.1 instead of HTTP/2.
The primary reason for Nginx not serving content over HTTP/2 is often a misconfiguration in the server settings. HTTP/2 support must be explicitly enabled in the Nginx configuration file. Additionally, the client (browser) must support HTTP/2, and the server must be using SSL/TLS, as HTTP/2 over Nginx requires a secure connection.
To resolve the issue of Nginx not serving content over HTTP/2, follow these steps:
Ensure you are using a version of Nginx that supports HTTP/2. Run the following command to check your Nginx version:
nginx -v
HTTP/2 support was added in Nginx 1.9.5, so ensure your version is at least this or newer.
Edit your Nginx configuration file, typically located at /etc/nginx/nginx.conf
or within the /etc/nginx/sites-available/
directory. Locate the server block for your site and add http2
to the listen
directive:
server {
listen 443 ssl http2;
server_name example.com;
# Other configurations
}
Ensure that the ssl
directive is also present, as HTTP/2 requires a secure connection.
After making changes, test the Nginx configuration for syntax errors:
nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Use browser developer tools or online services like KeyCDN HTTP/2 Test to verify that your site is now being served over HTTP/2.
For more detailed information on configuring Nginx with HTTP/2, refer to the Nginx HTTP/2 Module Documentation. Additionally, ensure your SSL/TLS configuration is secure by following best practices outlined in the Mozilla SSL Configuration Generator.
(Perfect for DevOps & SREs)
(Perfect for DevOps & SREs)