There is a new revision of this tutorial available for Debian 8 (Jessie).

Using ngx_pagespeed With nginx On Debian Jessie/testing

PageSpeed Beta is available 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 Jessie/testing.

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 Jessie/testing in /etc/apt/sources.list:

vi /etc/apt/sources.list

deb testing main contrib non-free
deb-src testing main contrib non-free

Update your packages list and install some prerequisites:

apt-get update

apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip curl libcurl4-openssl-dev libossp-uuid-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 2292
drwxr-xr-x 10 root root    4096 Feb  5 12:54 nginx-1.4.4
-rw-r--r--  1 root root 1568452 Jan  7 19:22 nginx_1.4.4-4.debian.tar.gz
-rw-r--r--  1 root root    2171 Jan  7 19:22 nginx_1.4.4-4.dsc
-rw-r--r--  1 root root  768217 Nov 21 16:28 nginx_1.4.4.orig.tar.gz

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

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

... and download the PageSpeed sources (you can check for the latest release on this page: - in this example the version is; if your version differs, make sure to replace it in the rest of this tutorial):

cd ngx_pagespeed-release-
tar -xzvf

Let's edit the debian/rules file:

vi /usr/src/pagespeed/nginx-1.4.4/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-release- \ to each of them, right before the $(CONFIGURE_OPTS) >$@ line, e.g. as follows:

config.status.full: config.env.full
        cd $(BUILDDIR_full) && CFLAGS="$(CFLAGS)" CORE_LINK="$(LDFLAGS)" ./configure  \
            --prefix=/usr/share/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=/run/ \
            --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_spdy_module \
            --with-http_stub_status_module \
            --with-http_ssl_module \
            --with-http_sub_module \
            --with-http_xslt_module \
            --with-ipv6 \
            --with-mail \
            --with-mail_ssl_module \
            --add-module=$(MODULESDIR)/nginx-auth-pam \
            --add-module=$(MODULESDIR)/nginx-dav-ext-module \
            --add-module=$(MODULESDIR)/nginx-echo \
            --add-module=$(MODULESDIR)/nginx-upstream-fair \
            --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module \
            --add-module=$(MODULESDIR)/ngx_pagespeed-release- \
            $(CONFIGURE_OPTS) >$@
        touch $@

Now let's build our new nginx packages:

cd /usr/src/pagespeed/nginx-1.4.4/ && 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 67260
drwxr-xr-x 10 root root     4096 Feb  5 13:18 nginx-1.4.4
-rw-r--r--  1 root root    67132 Feb  5 13:18 nginx_1.4.4-4_all.deb
-rw-r--r--  1 root root     4749 Feb  5 13:19 nginx_1.4.4-4_amd64.changes
-rw-r--r--  1 root root  1568452 Jan  7 19:22 nginx_1.4.4-4.debian.tar.gz
-rw-r--r--  1 root root     2171 Jan  7 19:22 nginx_1.4.4-4.dsc
-rw-r--r--  1 root root   768217 Nov 21 16:28 nginx_1.4.4.orig.tar.gz
-rw-r--r--  1 root root    79288 Feb  5 13:18 nginx-common_1.4.4-4_all.deb
-rw-r--r--  1 root root    78448 Feb  5 13:18 nginx-doc_1.4.4-4_all.deb
-rw-r--r--  1 root root  2520834 Feb  5 13:19 nginx-extras_1.4.4-4_amd64.deb
-rw-r--r--  1 root root 25480472 Feb  5 13:19 nginx-extras-dbg_1.4.4-4_amd64.deb
-rw-r--r--  1 root root  2372314 Feb  5 13:18 nginx-full_1.4.4-4_amd64.deb
-rw-r--r--  1 root root 23850670 Feb  5 13:19 nginx-full-dbg_1.4.4-4_amd64.deb
-rw-r--r--  1 root root  2349992 Feb  5 13:19 nginx-light_1.4.4-4_amd64.deb
-rw-r--r--  1 root root  3416998 Feb  5 13:19 nginx-light-dbg_1.4.4-4_amd64.deb
-rw-r--r--  1 root root  2384044 Feb  5 13:19 nginx-naxsi_1.4.4-4_amd64.deb
-rw-r--r--  1 root root  3581602 Feb  5 13:19 nginx-naxsi-dbg_1.4.4-4_amd64.deb
-rw-r--r--  1 root root   309478 Feb  5 13:18 nginx-naxsi-ui_1.4.4-4_all.deb

We can now install nginx with PageSpeed support as follows:

dpkg --install nginx-common_1.4.4-4_all.deb nginx_1.4.4-4_all.deb nginx-full_1.4.4-4_amd64.deb

/etc/init.d/nginx restart

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.4.4
TLS SNI support enabled
configure arguments: --prefix=/usr/share/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=/run/ --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_spdy_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-mail --with-mail_ssl_module --add-module=/usr/src/pagespeed/nginx-1.4.4/debian/modules/nginx-auth-pam --add-module=/usr/src/pagespeed/nginx-1.4.4/debian/modules/nginx-dav-ext-module --add-module=/usr/src/pagespeed/nginx-1.4.4/debian/modules/nginx-echo --add-module=/usr/src/pagespeed/nginx-1.4.4/debian/modules/nginx-upstream-fair --add-module=/usr/src/pagespeed/nginx-1.4.4/debian/modules/ngx_http_substitutions_filter_module --add-module=/usr/src/pagespeed/nginx-1.4.4/debian/modules/ngx_pagespeed-release-


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 reload nginx:

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

/etc/init.d/nginx reload

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:/usr/src/pagespeed# 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

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

vi /etc/nginx/sites-available/

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 {
    deny all;
  location /ngx_pagespeed_global_statistics {
    deny all;
  location /ngx_pagespeed_message {
    deny all;
  location /pagespeed_console {
    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:

Don't forget to restart nginx afterwards:

/etc/init.d/nginx reload

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:

0 Comment(s)

Add comment