How can you enable and configure HTTP/2 in Nginx? nginx

How can you enable and configure HTTP/2 in Nginx?


Nov. 13, 2023

How to Enable and Configure HTTP/2 in Nginx

HTTP/2 is a major revision of the HTTP protocol, bringing significant improvements in performance and efficiency. Nginx, a popular web server, has support for HTTP/2, allowing you to take advantage of these benefits. This article will guide you on how to enable and configure HTTP/2 in Nginx.

Step 1: Verify Nginx Version

Before enabling HTTP/2, it's important to ensure that you have a compatible version of Nginx. HTTP/2 support was introduced in Nginx version 1.9.5 and later. To check the version of Nginx installed on your server, run the following command in a terminal:

nginx -v

If your version is lower than 1.9.5, you'll need to update Nginx to a newer version that supports HTTP/2.

Step 2: Edit Nginx Configuration

Once you have a compatible version of Nginx, you'll need to edit the Nginx configuration file to enable HTTP/2. Locate the Nginx configuration file, typically found at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf, and open it in a text editor.

Within the http block, add the following line to enable HTTP/2:

http2 on;

Save the changes and exit the text editor.

Step 3: Test Nginx Configuration

Before restarting Nginx, it's crucial to test the configuration file to identify any syntax errors. Run the following command in a terminal:

nginx -t

If the configuration file contains no errors, you'll see a "syntax is ok" message. If you encounter any errors, review your changes and correct them accordingly.

Step 4: Restart Nginx

Once the configuration has been tested and verified, it's time to restart Nginx to apply the changes. Use the following command to restart Nginx:

sudo systemctl restart nginx

If you encounter any issues during the restart process, review the error logs for further information on what might be causing the problem.

Step 5: Verify HTTP/2 Enabled

To confirm that HTTP/2 is enabled and functioning correctly, you can use various tools. One such tool is the online HTTP/2 test available at https://www.http2.pro/. Enter your website URL and click the "Start Test" button. The test will provide information on whether your website successfully supports HTTP/2.

Additionally, you can use your web browser's developer tools to inspect the network requests. Look for the h2 or http/2 protocol indicator, which confirms that the website is using HTTP/2.

Conclusion

Enabling and configuring HTTP/2 in Nginx can significantly improve your website's performance and efficiency. By following the steps outlined in this article, you should be able to enable and enjoy the benefits of HTTP/2. Remember to keep your Nginx server up to date to ensure compatibility with the latest improvements in HTTP technology.

nginx