There is a new version of this tutorial available for Debian 11 (Bullseye).

Using ngx_pagespeed With nginx On Debian Wheezy

Google has just released PageSpeed Beta for nginx, the nginx equivalent of mod_pagespeed for Apache. This module applies web performance best practices to pages, and associated assets (CSS, JavaScript, images) and therefore speeds up your web site and reduces load times. This tutorial explains how to use PageSpeed with nginx on Debian Wheezy.

I do not issue any guarantee that this will work for you!

 

1 Building nginx With PageSpeed

nginx does not support dynamic loading of modules, therefore we must rebuild it with PageSpeed support. We will build nginx as a .deb package so that we can replace our existing nginx installation with it (or easily install it on other systems).

Create the build directory first:

cd /usr/src
mkdir pagespeed && cd pagespeed

Make sure you have deb and deb-src lines for Wheezy in /etc/apt/sources.list:

vi /etc/apt/sources.list
[...]
deb http://ftp.de.debian.org/debian/ wheezy main
deb-src http://ftp.de.debian.org/debian/ wheezy main
[...]

Update your packages list and install some prerequisites:

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

Download the nginx sources and the build dependencies:

apt-get source nginx
apt-get build-dep nginx

Let's check our nginx version:

ls -l
root@server1:/usr/src/pagespeed# ls -l
total 2044
drwxr-xr-x 10 root root    4096 Apr 29 13:03 nginx-1.2.1
-rw-r--r--  1 root root 1360507 Aug  5  2012 nginx_1.2.1-2.2.debian.tar.gz
-rw-r--r--  1 root root    2740 Aug  5  2012 nginx_1.2.1-2.2.dsc
-rw-r--r--  1 root root  718161 Jun 27  2012 nginx_1.2.1.orig.tar.gz
root@server1:/usr/src/pagespeed#

Our nginx version is 1.2.1. Let's go to the debian/modules directory of the downloaded nginx sources...

cd /usr/src/pagespeed/nginx-1.2.1/debian/modules

... and download the PageSpeed sources:

git clone https://github.com/pagespeed/ngx_pagespeed.git

Let's edit the debian/rules file:

vi /usr/src/pagespeed/nginx-1.2.1/debian/rules

In that file you will find four config.status sections (config.status.full, config.status.light, config.status.extras, config.status.naxsi). Add the line --add-module=$(MODULESDIR)/ngx_pagespeed \ to each of them, right before the $(CONFIGURE_OPTS) >$@ line, e.g. as follows:

[...]
config.status.full: config.env.full config.sub config.guess
        cd $(BUILDDIR_full) && CFLAGS="$(CFLAGS)" CORE_LINK="$(LDFLAGS)" ./configure  \
            --prefix=/etc/nginx \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --http-client-body-temp-path=/var/lib/nginx/body \
            --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
            --http-log-path=/var/log/nginx/access.log \
            --http-proxy-temp-path=/var/lib/nginx/proxy \
            --http-scgi-temp-path=/var/lib/nginx/scgi \
            --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
            --lock-path=/var/lock/nginx.lock \
            --pid-path=/var/run/nginx.pid \
            --with-pcre-jit \
            --with-debug \
            --with-http_addition_module \
            --with-http_dav_module \
            --with-http_geoip_module \
            --with-http_gzip_static_module \
            --with-http_image_filter_module \
            --with-http_realip_module \
            --with-http_stub_status_module \
            --with-http_ssl_module \
            --with-http_sub_module \
            --with-http_xslt_module \
            --with-ipv6 \
            --with-sha1=/usr/include/openssl \
            --with-md5=/usr/include/openssl \
            --with-mail \
            --with-mail_ssl_module \
            --add-module=$(MODULESDIR)/nginx-auth-pam \
            --add-module=$(MODULESDIR)/nginx-echo \
            --add-module=$(MODULESDIR)/nginx-upstream-fair \
            --add-module=$(MODULESDIR)/nginx-dav-ext-module \
            --add-module=$(MODULESDIR)/ngx_pagespeed \
            $(CONFIGURE_OPTS) >$@
        touch $@
[...]

Now let's build our new nginx packages:

cd /usr/src/pagespeed/nginx-1.2.1/ && dpkg-buildpackage -b

Afterwards, let's take a look at the generated packages:

cd /usr/src/pagespeed
ls -l
root@server1:/usr/src/pagespeed# ls -l
total 35464
drwxr-xr-x 10 root root    4096 Apr 29 13:12 nginx-1.2.1
-rw-r--r--  1 root root   61006 Apr 29 13:12 nginx_1.2.1-2.2_all.deb
-rw-r--r--  1 root root    5143 Apr 29 13:12 nginx_1.2.1-2.2_amd64.changes
-rw-r--r--  1 root root 1360507 Aug  5  2012 nginx_1.2.1-2.2.debian.tar.gz
-rw-r--r--  1 root root    2740 Aug  5  2012 nginx_1.2.1-2.2.dsc
-rw-r--r--  1 root root  718161 Jun 27  2012 nginx_1.2.1.orig.tar.gz
-rw-r--r--  1 root root   72632 Apr 29 13:12 nginx-common_1.2.1-2.2_all.deb
-rw-r--r--  1 root root   74042 Apr 29 13:12 nginx-doc_1.2.1-2.2_all.deb
-rw-r--r--  1 root root 4140470 Apr 29 13:12 nginx-extras_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 6075706 Apr 29 13:12 nginx-extras-dbg_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 3931344 Apr 29 13:12 nginx-full_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 4609688 Apr 29 13:12 nginx-full-dbg_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 3815222 Apr 29 13:12 nginx-light_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 3546806 Apr 29 13:12 nginx-light-dbg_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 3853762 Apr 29 13:12 nginx-naxsi_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root 3673646 Apr 29 13:12 nginx-naxsi-dbg_1.2.1-2.2_amd64.deb
-rw-r--r--  1 root root  342008 Apr 29 13:12 nginx-naxsi-ui_1.2.1-2.2_all.deb
root@server1:/usr/src/pagespeed#

We can now install nginx with PageSpeed support as follows:

dpkg --install nginx-common_1.2.1-2.2_all.deb nginx_1.2.1-2.2_all.deb nginx-full_1.2.1-2.2_amd64.deb

Let's check if the PageSpeed module was built successfully:

nginx -V

You should see the ngx_pagespeed module in the output:

root@server1:/usr/src/pagespeed# nginx -V
nginx version: nginx/1.2.1
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module
--with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module
--with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module
--add-module=/usr/src/pagespeed/nginx-1.2.1/debian/modules/nginx-auth-pam --add-module=/usr/src/pagespeed/nginx-1.2.1/debian/modules/nginx-echo
--add-module=/usr/src/pagespeed/nginx-1.2.1/debian/modules/nginx-upstream-fair --add-module=/usr/src/pagespeed/nginx-1.2.1/debian/modules/nginx-dav-ext-module
--add-module=/usr/src/pagespeed/nginx-1.2.1/debian/modules/ngx_pagespeed
root@server1:/usr/src/pagespeed#

 

2 Configuring PageSpeed

To enable PageSpeed, open /etc/nginx/nginx.conf...

vi /etc/nginx/nginx.conf

... and add the lines pagespeed on; and pagespeed FileCachePath /var/ngx_pagespeed_cache; before any vhosts:

[...]
        pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
[...]

Create the cache directory and restart nginx (please note that whenever you modify the PageSpeed configuration, no matter if in the main nginx configuration or in a vhost, you must restart nginx - a simple reload does not work):

mkdir /var/ngx_pagespeed_cache
chown -R www-data:www-data /var/ngx_pagespeed_cache
/etc/init.d/nginx restart
root@server1:~# /etc/init.d/nginx restart
Restarting nginx: Setting option from ("on")
Setting option from ("FileCachePath", "/var/ngx_pagespeed_cache")
nginx.
root@server1:~#

Let's load a page and check if PageSpeed is mentioned in the output:

curl -I -p http://localhost|grep X-Page-Speed
root@server1:~# curl -I -p http://localhost|grep X-Page-Speed
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
X-Page-Speed: 1.5.27.1-2845
root@server1:~#

Now we can configure PageSpeed individually or each vhost, e.g. like this:

vi /etc/nginx/sites-available/example.com.vhost
server {
[...]
  # let's speed up PageSpeed by storing it in the super duper fast memcached
  pagespeed MemcachedThreads 1;
  pagespeed MemcachedServers "localhost:11211";

  # Filter settings
  pagespeed RewriteLevel CoreFilters;
  pagespeed EnableFilters collapse_whitespace,remove_comments;

  #  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$" {
  }
  location /ngx_pagespeed_statistics {
    allow 127.0.0.1; deny all;
  }
  location /ngx_pagespeed_message {
    allow 127.0.0.1; deny all;
  }
[...]
}

The important line is the pagespeed EnableFilters line which tells PageSpeed which filters it should apply. You can find a list of all filters here: http://ngxpagespeed.com/ngx_pagespeed_example/

Don't forget to restart nginx afterwards:

/etc/init.d/nginx restart

When you open a page now and take a look at the headers (e.g. with the Live HTTP headers add-on for FireFox), you should see an x-page-speed line:

You can check the HTML sources of your pages to see if the PageSpeed filters work as expected.

 

 

About The Author

Falko Timme is the owner of nginx WebhostingTimme Hosting (ultra-fast nginx web hosting). He is the lead maintainer of HowtoForge (since 2005) and one of the core developers of ISPConfig (since 2000). He has also contributed to the O'Reilly book "Linux System Administration".

Share this page:

4 Comment(s)