Enable HTTP2 for WordPress on nginx Ubuntu 14.04

HTTP2 is enabled on mainline version of nginx (v1.9.x), while Ubuntu 14.04 is using nginx 1.4.6.
To enable HTTP2, the simplest way is to upgrade nginx to the mainline version.
But I met several issues during the upgrade, and here’re the steps, the issues and solutions.

Upgrade to mainline nginx

Let’s use nginx’s official pre-built packages.

  • Add below lines into /etc/apt/sources.list.d/nginx.list
    sudo sh -c 'echo deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx >> /etc/apt/sources.list.d/nginx.list'
    sudo sh -c 'echo deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx >> /etc/apt/sources.list.d/nginx.list'
  • Add nginx apt key
    wget -q -O- http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
  • Update ans install nginx
    sudo apt-get update
    sudo apt-get install nginx

However, it fails with error that it tries to overwrite nginx-common’s file:

dpkg: error processing archive /var/cache/apt/archives/nginx_1.9.11-1~trusty_amd64.deb (--unpack):
 trying to overwrite '/usr/share/nginx/html/index.html'

The solution is to remove old nginx on Ubuntu and re-install mainline nginx.

sudo apt-get remove nginx-common
sudo apt-get install nginx

Now nginx is upgraded to 1.9.11

Config file changes

  1.   Previously the site config files are stored in /etc/nginx/site-available, and the enabled sites are soft-linked in /etc/nginx/site-enabled/.
    But mainline nginx only load config files in /etc/nginx/conf.d/*.conf.
    So either copy the old config files to conf.d, or add include /etc/nginx/sites-enabled/ in nginx.conf.
  2. Mainline nginx’s user is nginx, but Ubuntu uses www-data, so change it in nginx.conf.
    #user nginx;
    user www-data;
  3. Change spdy to http2 in site config.

Reload nginx, and now the site’s HTTP2 is working.
However, try to open any page of WordPress, it just shows a blank page without any error. The log shows HTTP 200 OK.

Blank page issue

After googling the issue, it’s found that a missing FastCGI param is the root cause.
Add below line in /etc/nginx/fastcgi_params:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Reload nginx again, and now everything works fine.