To enable HTTP/2 in Nginx, follow these steps:
Make sure your Nginx version supports HTTP/2: HTTP/2 is supported in Nginx version 1.9.5 or later. Check your version with:
If it's older than 1.9.5, you might need to update Nginx.
Edit Nginx Configuration:
Open the Nginx configuration file. The location of this file depends on your system, but common paths are /etc/nginx/nginx.conf
or /etc/nginx/sites-available/default
.
Use a text editor like nano
to open the file:
Enable HTTP/2 for your server block:
In the server
block for your site, modify the listen
directive to enable HTTP/2 by adding the http2
option. It should look like this:
If you're using HTTP over port 80, it's recommended to redirect traffic to HTTPS, as HTTP/2 requires SSL/TLS.
Check Nginx Configuration: Before restarting Nginx, ensure there are no syntax errors in the configuration:
Restart Nginx: If the configuration test passes, restart Nginx to apply the changes:
Verify HTTP/2 is working:
You can verify that HTTP/2 is enabled by inspecting the response headers in a browser or using curl
:
Look for HTTP/2
in the response headers.
That’s it! Your Nginx server should now be serving your site over HTTP/2.