Speedup Your Nginx

Pagespeed speeds up your site and reduces page load time. Ngx_pagespeed is an open-source module that automatically optimizes web server’s performance by reducing the resource size, load time of pages and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or data flow.

By Default , Nginx does not support dynamic loading of modules available in other web servers such as Apache. Therefore, we have to rebuild it from source to enable this module.

In this blog, we will speed up nginx by adding ngx_pagespeed module on ubuntu 14.04. Here we go !!

First of all, we have to update the list of the available packages, run the following command :-

sudo apt-get update

Install Nginx along with all the dependencies :-

sudo apt-get build-dep nginx

Next, we have to download the Nginx source package in home directory (change location accordingly) with the following command :-

cd ~

sudo apt-get source nginx

Run ‘ls’ command to see the content, the result should look like this :-

nginx-1.4.6 nginx_1.4.6-1ubuntu3.3.debian.tar.gz nginx_1.4.6-1ubuntu3.3.dsc nginx_1.4.6.orig.tar.gz

To start adding the ngx_pagespeed module, you first need to go to the modules folder within the extracted folder nginx-1.4.6 :-

cd nginx-1.4.6/debian/modules

In this directory, download the latest ngx_pagespeed source archive from its Github repository with the command:-

sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/master.zip

Unzip it :-

unzip master.zip

This will create a new directory called ngx_pagespeed-master inside your ~/nginx-1.4.6/debian/modules directory.For convenience rename this directory to just ngx_pagespeed :-

sudo mv ngx_pagespeed-master ngx_pagespeed  

Download the PageSpeed Optimization Libraries (psol) which are required for the compilation. Run the following commands :-

cd ngx_pagespeed

sudo wget https://dl.google.com/dl/page-speed/psol/

sudo tar -xzvf  

At this point we are ready to customize the compilation rules and include ngx_pagespeed in the installation. For this purpose edit the file ~/nginx-1.4.6/debian/rules :-

sudo nano ~/nginx-1.4.6/debian/rules

At the end of the light_configure_flags configuration block, add the line:-

--add-module=$(MODULESDIR)/ngx_pagespeed  \


Note:- Please don’t forget to add a backslash (\) at the end of the row.

The scenario should look like this :-


There are five different scenarios for building Nginx’s packages: core, full, light, extras and naxsi.You could add the same line to the other build scenarios too if you find a different Nginx setup more convenient.

Next,we have to increase the source package version. For this edit the changelog file :-

sudo nano  ~/nginx-1.4.6/debian/changelog

Add a custom tag such as pagespeed at the very first line of  changelog file containing the version number like this :-

nginx (1.4.6-1ubuntu3.3-pagespeed) trusty-proposed; urgency=medium

Now you have customized the build to include the ngx_pagespeed module, you are ready to build Nginx.Go to the directory ~/nginx-1.4.6/ and run the command to build the new custom Nginx binary packages :-

After the build completes, see the content of root.You should find a lot of .deb packages. The ones you need are called nginx-common_1.4.6-1ubuntu3.3-pagespeed_all.deb (containing the common Nginx files) and nginx-light_1.4.6-1ubuntu3.3-pagespeed_amd64.deb (containing your custom light server).

To install your custom Nginx with pagespeed module run the command :-

sudo dpkg -i nginx-common_1.4.6-1ubuntu3.3-pagespeed_all.deb nginx-light_1.4.6-1ubuntu3.3-pagespeed_amd64.deb

You now have Nginx installed. The next step is to enable the ngx_pagespeed module.Before enabling the module, you have to create a folder, where it will cache the files for your website :-

sudo mkdir -p /var/ngx_pagespeed_cache

Change the ownership of this folder to the nginx user :-

sudo chown -R www-data:www-data /var/ngx_pagespeed_cache

Add the following lines to the http block of nginx’s main configuration file /etc/nginx/nginx.conf :-

The scenario should look like this :-

Also, you need to add pagespeed configuration lines to every server block file located in /etc/nginx/sites-available. For example, edit the /etc/nginx/sites-available/default file :-

Add the following to the end of the server block :-

The above pagespeed configuration lines ensure that pagespeed will optimize every site’s resources.

Finally, restart Nginx server :-

Test the installation by running the Nginx binary like this :-

sudo /usr/sbin/nginx -V  

You should see the ngx_pagespeed module listed among the other modules :-

After that check for the X-Page-Speed header like this :-

If the ngx_pagespeed module works fine, you should see it in the output along with its version :-


Category(s) Uncategorized
. . .


Add Your Comment

Be the first to comment.