Nginx / Server Setup

How to install Google PageSpeed (ngx_pagespeed) module for NGINX in RHEL 8 / CentOS 8

by , , revisited on


NGINX is an open source web server well known for its high performance and the vast array of features available through modules.

The PageSpeed module for NGINX was developed by Google. Its function is optimizing website static assets as well as HTML output in a way that pages are loaded faster. Some of the techniques implemented by PageSpeed module are:

  • Serving images in next-generation WebP format
  • Minifying CSS and Javascript files
  • Extending cache lifetime (Far Future Expires headers) for static files

The PageSpeed module is available for both Apache and NGINX web servers. The common name is PageSpeed module. It is usually named ModPageSpeed in context of Apache web server. And specifically for NGINX web server, it is usually named as ngx_pagespeed.

Compile from source? No, thanks

When you need to install ngx_pagespeed, the official documentation is, unfortunately, not your best friend. That is, because they recommend compiling from source – a practice very well risky for production servers. Not to say that it will immerse you into maintenance nightmare whenever either new versions of NGINX or PageSpeed module are released. Compile, recompile.. compile again… Don’t.

Install ngx_pagespeed from package repository

Meet easy, well-maintained installation of PageSpeed module, available with a couple of dnf commands. And upgrades are only one command away.

You can use our repository to easily install RPM package for dynamic PageSpeed module for Nginx (ngx_pagespeed).

The dynamic module is compatible with the latest stable nginx for RHEL 8 / CentOS 8.

Step 1. Add GetPageSpeed extras YUM repository

First, run the following command to add our repository:

sudo dnf install https://extras.getpagespeed.com/release-el8-latest.rpm

Step 2. Install nginx and ngx_pagespeed

Install latest stable NGINX:

sudo dnf --disablerepo=rhel-8-for-x86_64-appstream-rpms install nginx

Then, all you have to do to install the PageSpeed module:

yum install nginx-module-pagespeed

Follow the installation prompt to import GPG public key that is used for verifying packages.

Step 3. Enable the module

Next, enable your nginx to load the pagespeed dynamic module by editing nginx configuration. Simply follow the installer’s suggestion:

----------------------------------------------------------------------

The PageSpeed dynamic module for nginx has been installed.
To enable this module, add the following to /etc/nginx/nginx.conf
and reload nginx:

    load_module modules/ngx_pagespeed.so;

Please refer to the module documentation for further details:
https://developers.google.com/speed/pagespeed/module/configuration

----------------------------------------------------------------------

3.1. Configure Pagespeed cache backend

PageSpeed module needs to store its optimized resources somewhere. This is called cache backend.

Typically, file-based cache backend is enough, provided that you have SSD. Put the following line within http { ... } section of nginx.conf for file based cache:

pagespeed FileCachePath /var/cache/pagespeed; 

3.2. Enable Pagespeed for all websites or individual ones

Finally, enable PageSpeed globally or for specific website with pagespeed on; inside http { ... } or server { ... }, respectively.

Your complete nginx.conf will look like this:

... other directives    

load_module modules/ngx_pagespeed.so;

http {
    pagespeed FileCachePath /var/cache/pagespeed; 
    pagespeed on; # Enables pagespeed for each website
    ... other directives
}

Supported Nginx versions: Nginx stable from either official Nginx or GetPageSpeed repositories.

Caveats

We have mentioned some techniques that PageSpeed module uses in order to optimize performance of your pages. But some things have to be taken into account in how you use ngx_pagespeed.

CPU load

PageSpeed module does its optimizations “on the fly”. Which means it will, sure enough, increase your CPU load. Even when every static asset is already optimized, ngx_pagespeed will still have to parse and analyze HTML of your pages in order to see if there are new assets appeared for optimization.

You may reduce the CPU load by configuring Downstream Caching, however this has “side effects” of its own:

you may be willing to give up the benefit of the beaconing-dependent filters in exchange for never intentionally bypassing the cache

I/O impact with LoadFromFile

It is important to note the in order to extend cache lifetime of static assets, PageSpeed does stat checks for each static file it optimized on a given page, on every page request (see issue). That may have a negative performance impact if your disk performance isn’t optimal.

Brotli support

As it stands, PageSpeed module does not support Brotli compression internally. That is, you can still use it along with Brotli NGINX module, but you will have to disable internal PageSpeed compression via:

pagespeed HttpCacheCompressionLevel 0;

SELinux compatibility

SELinux module dependency package will be automatically installed for you. So no worries on that side.

Tip: if you have SELinux enabled, you may want to check our post about SELinux configuration for nginx.

Alternatives?

You can optimize your website’s client-side performance without ngx_pagespeed if you put some effort into it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.