Installing the PageSpeed module for Nginx on Debian 9

How to compile Nginx with ngx_pagespeed for automatic website performance optimization.

ngx_pagespeed is an Nginx module that automatically optimizes your web pages — compressing images, minifying CSS and JavaScript, managing caching, and more — without touching a single line of your site's code.

Before you start

This guide involves recompiling Nginx from source. Make sure you have a backup of your current Nginx configuration and note down your existing compile flags — you'll need them.

Installing build dependencies

sudo apt-get build-dep nginx
sudo apt-get update
sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev

Setting up a working directory

mkdir ~/custom-nginx
cd ~/custom-nginx

Downloading the Nginx source

Check which version of Nginx you're currently running:

nginx -v
nginx version: nginx/1.18.0

Download the matching source tarball from the official Nginx site:

sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
sudo tar zxvf nginx-1.18.0.tar.gz

Setting up ngx_pagespeed

Navigate into the Nginx modules directory:

cd nginx-1.18.0/src/http/modules/

Download ngx_pagespeed from its GitHub repository:

sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/master.zip
sudo unzip master.zip
sudo mv incubator-pagespeed-ngx-master ngx_pagespeed
cd ngx_pagespeed

Download the PageSpeed Optimization Libraries (psol) — required for compilation:

sudo wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
sudo tar -xzvf 1.13.35.2-x64.tar.gz

Compiling Nginx with the pagespeed module

Head back to the Nginx source directory:

cd ~/custom-nginx/nginx-1.18.0/

First, check the compile flags your current Nginx was built with — you'll need to replicate them:

nginx -V

Run ./configure with those same flags, adding --add-module at the end:

sudo ./configure \
  --prefix=/etc/nginx \
  --sbin-path=/usr/sbin/nginx \
  --modules-path=/usr/lib/nginx/modules \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/run/nginx.lock \
  --http-client-body-temp-path=/var/cache/nginx/client_temp \
  --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
  --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
  --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
  --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
  --user=nginx \
  --group=nginx \
  --with-compat \
  --with-file-aio \
  --with-threads \
  --with-http_addition_module \
  --with-http_auth_request_module \
  --with-http_dav_module \
  --with-http_flv_module \
  --with-http_gunzip_module \
  --with-http_gzip_static_module \
  --with-http_mp4_module \
  --with-http_random_index_module \
  --with-http_realip_module \
  --with-http_secure_link_module \
  --with-http_slice_module \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_sub_module \
  --with-http_v2_module \
  --with-mail \
  --with-mail_ssl_module \
  --with-stream \
  --with-stream_realip_module \
  --with-stream_ssl_module \
  --with-stream_ssl_preread_module \
  --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' \
  --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \
  --add-module=./src/http/modules/ngx_pagespeed/

Always use the flags from your own nginx -V output — they may differ from the example above depending on how your system's package was built.

Build and install:

sudo make
sudo make install

Enabling ngx_pagespeed

Create a cache directory for the module:

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

Open the main Nginx config:

sudo nano /etc/nginx/nginx.conf

Add the following inside the http block:

##
# Pagespeed Settings
##
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

In each server block, add these location rules:

# Route requests for pagespeed-optimized resources correctly
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
    add_header "" "";
}
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

Restart Nginx to apply the changes:

sudo service nginx restart

Verifying the installation

Check that the module shows up in the build flags:

sudo /usr/sbin/nginx -V

You should see --add-module=./src/http/modules/ngx_pagespeed/ in the output.

Then confirm the module is actually running by inspecting the response headers:

curl -I -p YOUR_SERVER_IP

A successful installation will include an X-Page-Speed header in the response:

HTTP/1.1 200 OK
Server: nginx
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache

That header is your confirmation — ngx_pagespeed is installed and running.

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us