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:

https://developers.google.com/speed/pagespeed/module/config_filters

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.

https://github.com/pagespeed/ngx_pagespeed/blob/master/README.md

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:

http://articles.slicehost.com/2009/2/2/centos-adding-an-nginx-init-script

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
X-Page-Speed: 1.6.29.5-...

For more extensive tests, check:

https://github.com/pagespeed/ngx_pagespeed/wiki/Testing

Running on PageSpeed again

Just finished setting it up on Nginx.


amber:~ mojo$ curl -I 'http://overloaded.io/' | 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.3-3005

Since I moved from Apache to Nginx, PageSpeed was disabled, or better, not even installed.

Should be writing something about PageSpeed and the install process soon!

Finding process memory usage on Linux

Recently I came up with a script to quickly print the memory utilization of specific processes on Linux. It’s very simple, but proven to be very handy while I was moving and tuning my VPSs. It quickly showed me that Apache was taking pretty much all my memory and motivated the migration to nginx.

It’s currently set to get the Resident Set Size (RSS), but you can easily change it to use Virtual Memory (VSZ). Just change ‘rss’ to ‘vsz’ in the ps commands. Also, you can add more processes to the script. Just add a new line and change the process name.