This page looks best with JavaScript enabled

Enabling HTTP/2 in Nginx

 ·  β˜• 2 min read

This article mainly covers how to install Nginx from source on Linux and enable HTTP/2, and it also compares the speed of accessing this site over HTTP/1.1 versus HTTP/2.0.

1. Building and Installing Nginx

First you need to download Nginx and a few of its dependencies. Zlib compresses data when a web request is transferred, saving bandwidth; PCRE matches URLs with regular expressions for routing and dispatch; OpenSSL or LibreSSL, the secure sockets algorithm library, provides HTTPS support. The latest versions are used below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
wget http://nginx.org/download/nginx-1.11.1.tar.gz

wget http://zlib.net/zlib-1.2.8.tar.gz

wget http://ncu.dl.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz

wget http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.4.1.tar.gz

gunzip \*.gz

tar xvf nginx-1.11.1.tar zlib-1.2.8.tar   pcre-8.30.tar libressl-2.4.1.tar

When configuring, you need to enable at least the http_v2_module and http_ssl_module modules:

cd nginx-1.11.1
./configure --with-openssl=../libressl-2.4.1 --with-pcre=../pcre-8.30   --with-zlib=../zlib-1.2.8  --with-http_v2_module --with-http_ssl_module

You will see output like the following, which includes the paths to the configuration file and the logs.

nginx path prefix: “/usr/local/nginx”

nginx binary file: “/usr/local/nginx/sbin/nginx”

nginx modules path: “/usr/local/nginx/modules”

nginx configuration prefix: “/usr/local/nginx/conf”

nginx configuration file: “/usr/local/nginx/conf/nginx.conf”

nginx pid file: “/usr/local/nginx/logs/nginx.pid”

nginx error log file: “/usr/local/nginx/logs/error.log”

nginx http access log file: “/usr/local/nginx/logs/access.log”

nginx http client request body temporary files: “client_body_temp”

nginx http proxy temporary files: “proxy_temp”

nginx http fastcgi temporary files: “fastcgi_temp”

nginx http uwsgi temporary files: “uwsgi_temp”

nginx http scgi temporary files: “scgi_temp”

Then you can just install it.

1
2
make
make install

Nginx is installed to /usr/local/nginx by default. At this point you can check the Nginx version with a command.

1
/usr/local/nginx/sbin/nginx  -v

You will see output like the following.

nginx version: nginx/1.11.1

At this point you still cannot manage the Nginx state with service nginx start/stop/restart. You need to register it as a system service; here we simply replace the original Nginx with the version we just compiled.

1
2
3
service nginx stop
mv /usr/sbin/nginx /usr/sbin/nginx_bk
ln -s   /usr/local/nginx/sbin/nginx   /usr/sbin/nginx

2. Configuring Nginx

Note: using HTTP/2 requires an HTTPS connection. Configure the /usr/local/nginx/conf/nginx.conf file according to your service needs. Enabling HTTP/2 is very simple β€” you only need to replace the original listen 443 ssl with listen 443 ssl http2 and start nginx.

1
service nginx

3. Testing

Below is accessing this site without HTTP/2 enabled:

Below is accessing this site with HTTP/2 enabled:

There is an improvement in speed.


WeChat Official Account
WRITTEN BY
WeChat Official Account