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/1.9.32.6.tar.gz
sudo tar -xzvf 1.9.32.6.tar.gz
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 :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
config.status.light: config.env.light cd $(BUILDDIR_light) && ./configure \ $(common_configure_flags) \ --with-http_gzip_static_module \ --without-http_browser_module \ --without-http_geo_module \ --without-http_limit_req_module \ --without-http_limit_zone_module \ --without-http_memcached_module \ --without-http_referer_module \ --without-http_scgi_module \ --without-http_split_clients_module \ --without-http_ssi_module \ --without-http_userid_module \ --without-http_uwsgi_module \ --add-module=$(MODULESDIR)/nginx-echo \ --add-module=$(MODULESDIR)/ngx_pagespeed \ >$@ touch $@ |
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 :-
1 |
cd ~/nginx-1.4.6/ |
1 |
sudo dpkg-buildpackage -b |
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 :-
1 2 3 4 5 6 |
## # Pagespeed Settings ## pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; |
The scenario should look like this :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
... http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; ## # Pagespeed Settings ## pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; ... |
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 :-
1 |
sudo nano /etc/nginx/sites-available/default |
Add the following to the end of the server block :-
1 2 3 4 5 |
# Ensure requests for pagespeed optimized resources go to the pagespeed # handler and no extraneous headers get set. location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/ngx_pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon" { } |
The above pagespeed configuration lines ensure that pagespeed will optimize every site’s resources.
Finally, restart Nginx server :-
1 |
sudo service nginx restart |
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 :-
1 2 3 |
nginx version: nginx/1.4.6 ... --add-module=root/nginx-1.4.6/debian/modules/ngx_pagespeed |
After that check for the X-Page-Speed header like this :-
1 |
curl -I -p http://localhost| grep X-Page-Speed |
If the ngx_pagespeed module works fine, you should see it in the output along with its version :-
1 |
X-Page-Speed: 1.9.32.6-7321 |