Our Life TogetherThoughtsAdding PageSpeed to an Already-running NGINX Instance  —  { code }

How may you add a dynamic PageSpeed module to an already-built instance of NGINX?

For install automation, I’ve summarized the codes and minimal settings in this script. This Bash script was tested to work on the latest stable version of NGINX. If it’s your first time to compile this, feel free to execute the codes step-by-step. It will help you troubleshoot any peculiarities on your system.

This module was built by the script above: ngx_pagespeed.so. If you are on the latest stable version of NGINX, you may download that file and place it in your NGINX modules folder. Mine was at /usr/share/nginx/modules/.

Loading the module

To activate PageSpeed, add this line to NGINX’s main configuration file (mine was at /etc/nginx/nginx.conf), outside any block { ... }, on the top-most section of the config file:


load_module modules/ngx_pagespeed.so;
Configuring filters

What PageSpeed does to your served pages and assets may be fully customized via filters. There are a plethora of configurations to choose from. A pre-selected set of default filters may be activated through:


pagespeed RewriteLevel CoreFilters;

But don’t we normally want to fiddle with every little bit of our configuration? Here’s what’s included in my server block:

I also set up PageSpeed Admin Pages, which required the FileCachePath directive to be declared with it. While the Admin Pages location blocks can be retained inside the server block, Admin Pages are declared within the http block, which is outside the server block:

Brotli compression

Google has another open-source project for next-generation compression called Brotli. If you’re interested in installing a dynamic ngx_brotli module to your running NGINX server, you may wish to check this post: Adding Brotli to an Already-built NGINX Instance.

Updating the module

If your NGINX updates through a package manager like apt, updating NGINX will require that you update the modules as well. My current workaround is to disable automatic updates for nginx and nginx-extras through apt-mark’s hold command:


sudo apt-mark hold nginx nginx-extras

I’ll update this post once I’ve got the apt hook working that will run the above script before NGINX automatically upgrades. 🙂 For now, you may use this Bash script to automatically install ngx_pagespeed before you unhold NGINX for upgrade. Enjoy speed!

</>

Categories: Thoughts

Majal

Servant of the Creator happily married to Jehovah's slave girl.