How to Set Up Google PageSpeed Module in Nginx on Ubuntu 20.04
The loading speed of web site pages affects the user experience and the position of the site in search engines results. To analyze and speed up the loading of content, there is a Google PageSpeed module. In this tutorial, we will set up the Google PageSpeed Module in Nginx on Ubuntu 20.04.
Preparing to install the Google PageSpeed module
First, you must have the Nginx web server installed. If you don't have it for some reason, you can install it using the following command:
You also need the curl package to download the installation script and SSL libraries.
Check which version of Nginx and modules are installed.
Output:
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments:
...
The same version is best specified in the following command to minimize the risk when rebuilding Nginx. Here you can see which modules are installed now and add the necessary ones during the installation process.
Installing the Google PageSpeed module
In the case of Nginx, the PageSpeed module must be built from source. In addition, all dependencies must be installed. To simplify all these processes, the developers suggest using an installation script that will do all this. Nevertheless, it is better to test the operation of sites with this module after rebuilding Nginx on a test server and only then install it on a production server. Use the following command to rebuild Nginx with the Google PageSpeed module. Specify the Nginx version installed on the system in the --nginx-version parameter.
--nginx-version 1.18.0
To see all installation options, run:
During installation, all questions should be answered positively. After the next request, you can enter what other modules need to be included in the assembly.
arguments you would like to set? For example, if you would like
to build nginx with https support give --with-http_ssl_module
If you don't have any, just press enter.
>
The default set of parameters is shown below. It is worth using it for a basic installation or adding the modules you need to it.
Once the installation is complete, create the following symbolic link.
To verify that the installation was successful, enter:
You will see the Google PageSpeed module first among the configuration arguments:
configure arguments: --add-module=/root/incubator-pagespeed-ngx-latest-stable ...
Enabling Google PageSpeed Module
Now you need to enable the module in every virtual host on the server. Let's show this process using a default one as an example. Open the virtual host configuration:
Add these lines to the server section:
…
pagespeed on;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
…
}
Check configuration for errors:
If you don't have a website on this server yet, you need to create an index page for the default host with arbitrary content.
Now, restart Nginx.
Testing the Google PageSpeed Module
The easiest way to make sure that the Google PageSpeed module is working is to access our website using curl:
Output:
Server: nginx/1.18.0
Content-Type: text/html
Connection: keep-alive
Date: Wed, 13 Jan 2021 13:13:05 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache
The X-Page-Speed entry indicates that the Google PageSpeed module is running and shows its version.