Pagespeed: building Nginx with it on Ubuntu

Under: Blog

Tagged pagespeed perf
Posted 5 years ago

2016 note: these instructions are for v1.6 of Nginx. They are still valid, though, just replace the most recent version numbers of nginx and ngx_pagespeed in the appropriate places. You may like to switch to Nginx mainline at the same time.

Pagespeed is Google's web server module designed to speed up website response time and modify code to follow its Pagespeed ruleset (which you can test on the Pagespeed Insights page). In this post, I'll show you the why and how of getting ngx_pagespeed up and running on Nginx Ubuntu offical packages.

First the why - and quoting the module's documentation page:

PageSpeed speeds up your site and reduces page load time. This open-source webserver module automatically applies web performance best practices to pages and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or workflow.

That last part is the most important for the takeup of an external toolset!
ngx_pagespeed carries out the following automated tasks, which are all optional based on your requirements, the most important of which are:

  • inlines Javascript code

  • inline external CSS resources to reduce the number of external requests

  • caches the modified page to disk or Memcache

  • collapses whitespace

  • minifies Javascript and CSS

  • resizes images

  • lazyload images

  • remove comments (except conditional comments)

  • inserts DNS prefetch HTML tags

Importantly, you can turn on and off various rules to see how well your website performs and to get the best possible balance between load and performance. Certain rules in pagespeed have a higher probability than others of causing errors on pages - with this in mind you can trial various rules and determine their long term viability.

Finally, pagespeed includes a statistics backend that allows you to monitor the performance of various pages and pagespeed itself via a beacon inserted into the code.

Unlike Apache,  Nginx doesn't support dynamic loading of modules which means the module has to be compiled into Nginx. This may sound daunting but it's actually fairly simple to do and once complete you'll have  a package that can be installed onto an Ubuntu server. If you spot any errors below, leave a comment and I'll have a look.

Pagespeed will give a large performance boost to your website, smaller responses and shorter response times - you can also experiment with the various rules available to tweak your server setup for optimal performance.

Note that these instructions use the packages, not the ones from the Ubuntu or Debian repos. If you have those installed, and you install the packages built below, you will find that the resulting paths for nginx configuration may change and may result in Nginx not starting.
If you use the Ubuntu/Debian packages, build against the source of those.

Finally, remember to build on the same architecture as the target production server and test, test, test before rolling it out.


First up, ensure your package list in /etc/apt/source.list.d/nginx.list contains:

deb trusty nginx
deb-src trusty nginx

Install essential packages

sudo apt-get update
sudo apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev

Install nginx source to a directory in your ~

mkdir -p ~/tmp/nginx_source
cd ~/tmp/nginx_source
sudo apt-get source nginx
sudo apt-get build-dep nginx

Once that is complete, you should see these files

~/tmp/nginx_source$ ls -l
total 828
drwxr-xr-x 10 root root 4096 Feb 13 00:50 nginx-1.6.2
-rw-r--r-- 1 root root 6717 Sep 16 14:24 nginx_1.6.2-1~trusty.debian.tar.gz
-rw-r--r-- 1 root root 1436 Sep 16 14:24 nginx_1.6.2-1~trusty.dsc
-rw-r--r-- 1 root root 804164 Sep 16 14:24 nginx_1.6.2.orig.tar.gz

Download and build the pagespeed module

Follow part of these instructions here on building ngx_pagespeed module (but don't compile Nginx!).

The latest release at this date is

mkdir -p ~/tmp/ngx_pagespeed
cd ~/tmp/ngx_pagespeed
unzip release-${NPS_VERSION}

This will unzip to "ngx_pagespeed-release-${NPS_VERSION}-beta"
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
tar -xzvf ${NPS_VERSION}.tar.gz

The tar command extracts to psol/

Set up the Nginx source to build with ngx_pagespeed

Edit the Rules file for the Nginx package, replace 1.6.2 (current version at date) with the version of the source version installed
sudo vim ~/tmp/nginx_source/nginx-1.6.2/debian/rules

Add the module to both ./configure commands listed (i for insert in Vim)
--add-module=../../ngx_pagespeed/ngx_pagespeed-release- \

For example, I added that line here:
CFLAGS="" ./configure \
--add-module=../../ngx_pagespeed/ngx_pagespeed-release- \
--prefix=/etc/nginx \

Write and quit vim ( :wq)

Build the packages

cd ~/tmp/nginx_source/nginx-1.6.2
sudo dpkg-buildpackage -b

You should see a " + ngx_pagespeed was configured" line if you have good eyesight. If the build fails, check the errors, fix and try again.

Once package building is complete, look in the directory:

cd ~/tmp/nginx_source/
ls -la

~/tmp/nginx_source$ ls -l
total 23832
drwxr-xr-x 11 root root 4096 Feb 13 01:07 nginx-1.6.2
-rw-r--r-- 1 root root 1030 Feb 13 01:10 nginx_1.6.2-1~trusty_amd64.changes
-rw-r--r-- 1 root root 3162452 Feb 13 01:09 nginx_1.6.2-1~trusty_amd64.deb
-rw-r--r-- 1 root root 6717 Sep 16 14:24 nginx_1.6.2-1~trusty.debian.tar.gz
-rw-r--r-- 1 root root 1436 Sep 16 14:24 nginx_1.6.2-1~trusty.dsc
-rw-r--r-- 1 root root 804164 Sep 16 14:24 nginx_1.6.2.orig.tar.gz
-rw-r--r-- 1 root root 20358892 Feb 13 01:10 nginx-debug_1.6.2-1~trusty_amd64.deb

Install the package package you want to install

sudo dpkg -i nginx_1.6.2-1~trusty_amd64.deb

(Reading database ... NNNN files and directories currently installed.)
Preparing to unpack nginx_1.6.2-1~trusty_amd64.deb ...
Unpacking nginx (1.6.2-1~trusty) over (1.6.1-1~trusty) ...
Setting up nginx (1.6.2-1~trusty) ...


Run nginx -V to show that ngx_pagespeed is configured:
$ nginx -V
nginx version: nginx/1.6.2
built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
TLS SNI support enabled
configure arguments: --add-module=../../ngx_pagespeed/ngx_pagespeed-release- etc etc

Check config

$ sudo /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Configure servers

If all is well you can start configuring a server within the Nginx config for use with pagespeed. As a tip, I push all pagespeed config into a separate config file then include that file in servers that I want pagespeed to be enabled on.

Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments