PageSpeed Module on Nginx (ngx_pagespeed)

Not too long ago, during a server migration, I decided to move from Apache to Nginx, mostly because I think it’s a fun weekend project and maybe a little bit because I was looking to save some memory on my VPS. After a few hiccups, Nginx was running smoothly, serving the blog and a few other things.

Hearing Steve Sounders (@Souders) and Ilya Grigorik (@igrigorik) from Google talk about PageSpeed at Velocity Conference made me think about it again. I had the PageSpeed module configured on Apache, but didn’t bother to set it up on Nginx after the migration.

From the PageSpeed website:

PageSpeed speeds up your site and reduces page load time. This open-source webserver module automatically applies web performance best practices to pages and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or workflow.

Basically it does a lot of cool stuff, like minifying and combining JavaScript and CSS files, cache optimizations, inline resources, etc, everything directly on the web server layer, so you don’t have to worry about it at the application layer.

A complete list of filters that can be used with PageSpeed can be found at:

Some are safer than others, but generally speaking, they work really well.

I really don’t wan’t to go into install details, because they are really well described in the README file on GitHub.

The only detail I would like to bring up is regarding other Nginx binaries on the same server. Since the install process involves downloading and compiling Nginx, not downloading a binary from a repository, I will probably end up with multiple copies on your server. To avoid confusion, I prefer to remove repository versions before starting the setup.

After compiling and installing a new Nginx with PageSpeed, you will also need to update your nginx.conf and vhosts. Again, everything is described in detail in the README file.

Besides CoreFilters, I also enabled a few “optional” filters:

pagespeed EnableFilters canonicalize_javascript_libraries,make_google_analytics_async,lazyload_images,convert_jpeg_to_webp;

The names are pretty self-explanatory, but  a complete definition of the filters can be found in the config_filters page.

Another detail worth mentioning is that since Nginx is not coming from a repository, there’s no Init Script. I used the following example for CentOS:

And the full Init Script for CentOS:

After everything is done, you can test your installation simply by issuing a curl request:

$ curl -I 'http://localhost:8050/some_page/' | grep X-Page-Speed

For more extensive tests, check:

Tweet about this on TwitterShare on LinkedInShare on FacebookShare on Google+

Leave a Reply