Nginx + Google Pagespeed Debian Package

The normal Debian mirrors do not carry the nginx package with the Google Pagespeed Module. These instructions will allow you to create your own package that includes Pagespeed. The nginx package is from wheezy-backports which has a newer copy of nginx than the normal wheezy repo, remove -backports from any commands you run if you do not want that version.

You will need to install the following packages for the build process (you don’t need to build them on your web server, I use a server just for builds):

apt-get install devscripts dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

Install the dependencies needed for the build:

apt-get -t wheezy-backports build-dep nginx

Create a new directory and download a copy of the source:

mkdir /usr/src/nginx
cd /usr/src/nginx
apt-get -t wheezy-backports source nginx

To make things easier, set $nginx to the version of nginx:

root@apt:/usr/src/nginx# ls | grep nginx
root@apt:/usr/src/nginx# nginx="nginx-1.6.0"

Google Pagespeed

Go to the Google Pagespeed build page. Copy the text from the text box with the heading “Then download ngx_pagespeed” without the “cd ~”.

Go into the nginx modules directory and paste the commands from the Google site:

root@apt:/usr/src/nginx# cd ${nginx}/debian/modules
root@apt:/usr/src/nginx/nginx-1.6.0/debian/modules#   wget
--2014-05-18 10:07:20--
2014-05-18 10:07:27 (89.2 KB/s) - `' saved [114200]

root@apt:/usr/src/nginx/nginx-1.6.0/debian/modules#   unzip
   creating: ngx_pagespeed-release-
root@apt:/usr/src/nginx/nginx-1.6.0/debian/modules#   cd ngx_pagespeed-release-
root@apt:/usr/src/nginx/nginx-1.6.0/debian/modules/ngx_pagespeed-release-   wget
--2014-05-18 10:07:32--
root@apt:/usr/src/nginx/nginx-1.6.0/debian/modules/ngx_pagespeed-release-   tar -xzvf

Take note of the folder the ngx_pagespeed release zip file is extracted to (eg. ngx_pagespeed-release- Edit the /usr/src/nginx/${nginx}/debian/rules file so that the pagespeed module is compiled in. In the file search for light_configure_flags, full_configure_flags and extras_configure_flags. Each block will have one or more “–add-module” parts, add another pointing to the pagespeed folder:

light_configure_flags := \
      $(common_configure_flags) \
      --add-module=$(MODULESDIR)/ngx_pagespeed-release- \

full_configure_flags := \
      $(common_configure_flags) \
      --add-module=$(MODULESDIR)/ngx_pagespeed-release- \
      --add-module=$(MODULESDIR)/nginx-auth-pam \

extras_configure_flags := \
      $(common_configure_flags) \
      --add-module=$(MODULESDIR)/ngx_pagespeed-release- \
      --add-module=$(MODULESDIR)/nginx-echo \

Make sure the directory name is correct.

Building Package

Make sure you are in the directory where the source code was extracted and then run dch -i to increment the version and add an entry into the change log.

root@apt:/usr/src/nginx/nginx-1.6.0# cd /usr/src/nginx/${nginx}
root@apt:/usr/src/nginx/nginx-1.6.0# dch -i

The packages can then be built with dpkg-buildpackage -b:

root@apt:/usr/src/nginx/nginx-1.6.0# dpkg-buildpackage -b
dpkg-buildpackage: source package nginx
dpkg-buildpackage: source version 1.6.0-1~bpo70+1.1
dpkg-buildpackage: source changed by root 
dpkg-buildpackage: host architecture amd64
 dpkg-source --before-build nginx-1.6.0
 debian/rules clean
dh clean --with systemd
dpkg-deb: building package `nginx-naxsi-ui' in `../nginx-naxsi-ui_1.6.0-1~bpo70+1.1_all.deb'.
 dpkg-genchanges -b >../nginx_1.6.0-1~bpo70+1.1_amd64.changes
dpkg-genchanges: binary-only upload - not including any source code
 dpkg-source --after-build nginx-1.6.0
dpkg-buildpackage: binary only upload (no source included)

The /usr/src/nginx will have the .deb files that can be installed on the web servers as normal or uploaded to your own apt repoistory.

root@apt:/usr/src/nginx/nginx-1.6.0# find /usr/src/nginx -name *.deb
root@apt:/usr/src/nginx/nginx-1.6.0# dpkg -i /usr/src/nginx/nginx-full_1.6.0-1~bpo70+1.1_amd64.deb /usr/src/nginx/nginx-common_1.6.0-1~bpo70+1.1_all.deb /usr/src/nginx/nginx-doc_1.6.0-1~bpo70+1.1_all.deb


Edit /etc/nginx/nginx.conf and add the following:

http {
  pagespeed on;
  pagespeed FileCachePath /var/lib/nginx/pagspeed_cache;
  pagespeed PreserveUrlRelativity on;
  pagespeed CriticalImagesBeaconEnabled false;
  pagespeed XHeaderValue "Powered By ngx_pagespeed";

Edit any of sites (usually in /etc/nginx/sites-available/) and add the following:

server {
  location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
  location ~ "^/ngx_pagespeed_static/" { }
  location ~ "^/ngx_pagespeed_beacon$" { }
  location /ngx_pagespeed_statistics { allow; deny all; }
  location /ngx_pagespeed_global_statistics { allow; deny all; }
  location /ngx_pagespeed_message { allow; deny all; }
  location /pagespeed_console { allow; deny all; }
  location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }

Restart nginx and pagespeed will then be enabled.

Filters can be enabled specifically with pagespeed EnableFilters. This can be defined in the main configuration file or the site configuration, eg. in /etc/nginx/sites-available/

server {
  pagespeed EnableFilters inline_preview_images,lazyload_images,resize_mobile_images,rewrite_images,sprite_images,fallback_rewrite_css_urls,flatten_css_imports,outline_javascript;
  pagespeed EnableFilters inline_css,move_css_above_scripts,move_css_to_head,outline_css,rewrite_css,combine_css,combine_javascript,defer_javascript,inline_javascript,rewrite_javascript;

For more complete config options see the docs.

Posted in Linux, Web and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *