New Post has been published on Mocco
New Post has been published on http://mocco.sk/using-ngx_pagespeed-with-nginx-on-debian-wheezy/
Using ngx_pagespeed With nginx On Debian Wheezy
Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Follow me on Twitter
Last edited 04/29/2013
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:
[...] 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 install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev
Download the nginx sources and the build dependencies:
Letâs check our nginx version:
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:
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#
To enable PageSpeed, open /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:
(JavaScript must be enabled in your browser to view the large image as an image overlay.)
You can check the HTML sources of your pages to see if the PageSpeed filters work as expected.
Falko Timme is the owner of Timme 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â.
Check out the original source here.