How to Setup Nginx HTTP/2
HTTP/2, the successor of HTTP1.x, has been widely growing in popularity due to many of its performance benefits. As webpage sizes are growing and becoming more resource intensive, it is vital to stay current with performance optimization techniques and strategies. A few improvements of HTTP/2 compared to HTTP1.x include the following:
- Binary instead of textual
- Fully multiplexed
- Uses header compression
- Uses one connection for parralellism
- Implements server push
As the total HTTP/2 distribution rate increases, you may be interested in implementing HTTP/2 on your origin server. This article will walk through the steps of setting up Nginx HTTP/2 so that you can immediately start taking advantages of the performance improvements mentioned above.
How to setup Nginx HTTP/2
In order to setup and start using an Nginx HTTP/2 setup, there are a couple of things to first be aware of.
- To run HTTP/2 on Nginx you must use Nginx version 1.9.5 or later.
- Ensure that your site uses SSL/TLS encryption. Currently, browsers only support HTTP/2 on websites delivering content over HTTPS. For users who aren't yet using HTTPS, read our How to Migrate from HTTP to HTTPS guide.
You can check your current Nginx version by running the following command in your CLI.
nginx -V
This will display the version number as well as all configuration arguments. Within the configuration arguments, verify that you can find --with-http_v2_module
. Once you have confirmed you are running Nginx version 1.9.5 or later, continue with the following steps.
Open your
nginx.conf
file and navigate to the HTTPS server block.Your site should already be delivering content over SSL, therefore the change should be straightforward. Simply modify the following snippet:
listen 443 ssl;
to include http2 such as:
listen 443 ssl http2;
Your full HTTPS server block should now resemble the following:
server { listen 443 ssl http2; ssl_certificate server.crt; ssl_certificate_key server.key; }
Once complete, save your changes and reload Nginx with the following command.
service nginx reload
Your Nginx origin server should now be properly configured to deliver content through HTTP/2.
There are still some browsers which only offer partial HTTP/2 support as well as older browsers which do not support the new protocol at all.
For those browsers, Nginx makes use of the Application-Layer Protocol Negotiation (ALPN) extension. This TLS extension negotiates which protocol(s) should be used and tells browsers which do not yet support HTTP/2 to revert back to using HTTP1.x in order for content to be delivered to the client.
Check If You Are Delivering Content over HTTP/2
Once you have configured your Nginx HTTP/2 setup, you should double check that your assets are in fact being delivered via HTTP/2. This can be verified by using our HTTP/2 Test Tool. With this tool, simply enter the URL that you want to check against and click Test.
The tool will then check that your origin server is delivering assets over HTTP/2 and will also check if ALPN is supported. Now that you have properly configured your origin server and verified that it is delivering content via HTTP/2 you can begin taking advantage of its performance improvements. To learn more about HTTP/2, read our HTTP/2 blog article to see a speed comparison between HTTP1.x and HTTP/2 as well as how to further accelerate your assets with KeyCDN.