Enable HTTP/2 on Nginx

http2-nginx-crop-816x304

 

Experimental support for HTTP/2 became available in Nginx version 1.9.5 (mainline). It is really easy to enable, and I’ll show you how.

First of all: If you’re not running the latest version yet, I recommend that you upgrade Nginx to the latest version.


If you’re already running SPDY, please note that the SPDY module have been replaced with the HTTP/2 module in Nginx. Fortunately, to run HTTP/2 you just need to upgrade to Nginx 1.9.5 or later and replace spdy with http2 on your listen directive line.


Now, make sure your version of Nginx is compiled with HTTP/2 support:

$ nginx -V

Make sure you can find --with-http_v2_module somewhere in that output. If you don’t, upgrade to a build that supports HTTP/2 – like the link I posted above.

Now it is as simple as adding a single word to your Nginx config. Open the server block config for your HTTPS site (all implementations of HTTP/2 require HTTPS), and change this line:

listen 443 ssl;

to:

listen 443 ssl http2;

and reload your config:

$ service nginx reload

(If you haven’t enabled HTTPS yet, check this post: Securing Nginx with HTTPS)

Now all HTTP/2 enabled visitors should get your site delivered over HTTP/2, while older browsers get regular SSL (really: TLS). Major browsers supports HTTP/2 now.

The only thing left, is that you probably want to route all visitors that comes through standard HTTP on port 80 to your HTTPS port 443.

If you haven’t already done so, you are now ready to optimize HTTPS on Nginx.

FROM:https://bjornjohansen.no/enable-http2-on-nginx