How to Install and Configure Google PageSpeed Module for Nginx on Ubuntu 20.04
The loading speed of website pages plays a crucial role in both user experience and search engine rankings. One effective way to analyze and optimize performance is by using the Google PageSpeed Module. In this tutorial, you’ll learn how to install and configure the Google PageSpeed Module in Nginx on Ubuntu 20.04 to improve your website’s speed and efficiency.
In the Serverspace you can create a server with already installed app "Nginx".
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
Conclusion
Installing and configuring the Google PageSpeed Module for Nginx on Ubuntu 20.04 is a powerful way to boost your website’s performance, reduce page load times, and improve both user experience and SEO rankings. Although it requires rebuilding Nginx from source, the performance benefits gained through automatic content optimization are well worth the effort. By following this step-by-step guide, you now have PageSpeed up and running — ready to optimize images, compress HTML/CSS/JS, and deliver faster pages to your users.
FAQ
- What is the Google PageSpeed Module?
Google PageSpeed is an open-source module designed to automatically optimize your website’s content — including HTML, JavaScript, CSS, and images — to improve loading speed and user experience. - Is PageSpeed compatible with all versions of Nginx?
No. PageSpeed must be compiled into Nginx from source. It is important to use the correct version of Nginx during the build process to avoid compatibility issues. - Will installing PageSpeed affect my existing Nginx setup?
Yes, because Nginx must be rebuilt with the module. It’s highly recommended to test the setup on a staging or test server before applying changes to your production environment. - How can I verify that the PageSpeed module is working?
Use the curl -I command to view HTTP headers. If you see the X-Page-Speed header in the response, the module is active. - Where is PageSpeed’s cache stored?
By default, the file cache is stored at /var/cache/ngx_pagespeed/. You can change this path in your Nginx server block using the pagespeed FileCachePath directive. - Can I control which optimizations are applied?
Yes. You can use directives like pagespeed RewriteLevel OptimizeForBandwidth or enable specific filters manually. This allows fine-grained control over how PageSpeed optimizes your content.