Site icon GetPageSpeed

Install NGINX with ngx_pagespeed (Google PageSpeed) dynamic module in CentOS/RHEL, Amazon Linux and Fedora Linux

ngx_pagespeed

Nginx Pagespeed

You can use our repository to easily install the RPM package for the dynamic PageSpeed module for NGINX (ngx_pagespeed).
The dynamic module is compatible with the latest stable or mainline NGINX for maintained releases of CentOS/RHEL, Amazon Linux, and Fedora Linux.

Step 1. Add GetPageSpeed extras YUM repository

First, run the following command to add our repository:

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm

Step 2. Install nginx and ngx_pagespeed

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

sudo yum -y install nginx 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 PageSpeed dynamic module by editing its configuration.
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 the cache backend.

Typically, a 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 a website

Finally, enable PageSpeed for a specific website(s).

In every server block where PageSpeed needs to be enabled, add:

pagespeed on;

# 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 ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

That’s it!

Your complete nginx.conf may look like this:

# ... other directives ...   

load_module modules/ngx_pagespeed.so;

http {

    pagespeed FileCachePath /var/cache/pagespeed; 
    pagespeed SslCertDirectory /etc/pki/tls/certs;
    pagespeed SslCertFile /etc/pki/tls/cert.pem;

    # Enables pagespeed for each website:
    pagespeed on; 

    # ... other directives ...

    server {

        # ... other directives ...

        # 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 ~ "^/pagespeed_static/" { }
        location ~ "^/ngx_pagespeed_beacon$" { }             
    }
}

SELinux compatibility

A dependency SELinux module built for NGINX PageSpeed will be automatically installed for you at the same time with the PageSpeed module.

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

Caveats

We don’t know of any, at this time.

Alternatives?

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

Exit mobile version