Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
FREEDOM251

Freedom251 › Blog › Linux administration

How to Add ngx_pagespeed to Nginx (Package and Source-Build Methods)

By Freedom251 Team · · 12 min read

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a new Nginx deployment, install the maintained nginx-module-pagespeed package from the signed We-Amp repository whenever your operating system and Nginx build are supported. It is prebuilt, preserves the familiar pagespeed directives, and avoids compiling the PageSpeed Optimization Library or rebuilding a module after every Nginx upgrade. Compile a dynamic module only when you run a custom Nginx binary or an unsupported distribution.

What ngx_pagespeed is today

ngx_pagespeed is an Nginx module that applies PageSpeed rewrite filters to responses and their referenced assets. It can optimize HTML, CSS, JavaScript and images as requests pass through Nginx, while storing generated results in a local file cache. Existing configurations use directives such as pagespeed on;, pagespeed FileCachePath and pagespeed RewriteLevel.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The original open-source ngx_pagespeed repository is archived and no longer developed. The maintained continuation is mod_pagespeed 2.1; it keeps the established directive and filter names so an existing configuration can generally be retained. The legacy code has received no security patches since 2020 and lacks current features, making it appropriate only when you must preserve parity with an older deployment.

No dated, primary performance benchmark accompanies this installation guidance. Treat PageSpeed as a qualitative optimization layer and measure your own site before and after enabling filters. A response header proves that the worker is active, not that a particular page became faster under every network or cache condition.

Choose the installation method

Situation Recommended path Why
Ubuntu 22.04 or 24.04 (amd64 or arm64) Signed nginx-module-pagespeed package Prebuilt for the supported distribution Nginx ABI
Debian 11, 12 or 13 (amd64 or arm64) Signed package Package updates track the supported stock Nginx build
EL9 systems such as AlmaLinux, Rocky Linux or RHEL (x86_64 or aarch64) Signed package Maintained repository packages the module for these architectures
EL10 on x86_64 Signed package Supported by the current package matrix
Custom Nginx build, a third-party Nginx binary, or an operating-system/architecture combination outside those listed Compile a dynamic module against the exact Nginx source version you run Prevents an ABI mismatch between the module and your binary

The package is tied to the distribution’s stock Nginx build. A module installed for that build can be incompatible with an Nginx binary supplied by another repository, even when the Nginx version number appears identical. If your binary is custom or third-party, use the source-build route unless the package maintainer explicitly confirms compatibility.

There is no license fee for the module itself. We-Amp also offers optional support and hardened, attested builds; those are commercial services, and availability or pricing should be confirmed with the vendor rather than assumed to be part of the free package.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Prerequisites and planning

Before changing a production server, identify the Nginx binary that will load the module and the account that runs worker processes. Check whether Nginx came from the operating-system repository, a vendor repository, or a local build. The package method assumes the first case. A source build requires the exact Nginx release source, a compiler toolchain and development headers, and a repeatable way to rebuild after upgrades.

Back up /etc/nginx/nginx.conf and included files. Know which file your installation uses for module loading: Debian-derived packages often include files from /etc/nginx/modules-enabled, while other installations may require an explicit line in the main configuration. Plan a writable cache location with enough disk space. PageSpeed can still serve pages when its cache is empty, but an inaccessible cache causes failed or repeated processing and obscures whether the module is working.

Install the maintained package

1. Add the signed repository

Run the maintainer’s installer as root through sudo:

curl -fsSL https://packages.modpagespeed.com/install.sh | sudo sh

The command configures the package source and its signing key. Review your organization’s policy for piping a downloaded script to a shell; if required, download the script first, inspect it, and execute it locally with the same privileges. Confirm that the script selected the intended distribution and architecture before installing anything.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. Install the Nginx module

On Debian or Ubuntu:

sudo apt install nginx-module-pagespeed

On AlmaLinux, RHEL-family systems and other distributions using DNF:

sudo dnf install nginx-module-pagespeed

The package supplies the module shared object and is built for the distribution’s supported Nginx ABI. It avoids a local PSOL build and normally means you do not need to rebuild the module when the distribution updates Nginx within its supported packaging line. Keep normal package-update procedures in place so the module and Nginx remain on compatible versions.

3. Confirm that Nginx loads the module

Distribution packages commonly install a line in the main Nginx configuration:

load_module modules/ngx_pagespeed_module.so;

It must be in the main context, before events {} or http {}; it cannot be placed inside http {} or server {}. Packages on Debian-derived systems may place the line in a file under modules-enabled, which the main configuration includes automatically.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If your Nginx installation does not use modules-enabled, add the line yourself at the top level of /etc/nginx/nginx.conf. Use the module path installed by your package manager. Do not add a second load line if one already exists: duplicate loading can make Nginx fail its configuration test. Search every file included by nginx.conf when diagnosing a duplicate.

4. Add the minimal PageSpeed configuration

Put these directives in the http {} context, or in a file included from that context:

pagespeed on;
pagespeed FileCachePath /var/cache/ngx_pagespeed;
pagespeed RewriteLevel CoreFilters;

location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

CoreFilters is the safe starting level in the maintainer’s quick reference. It enables the standard set of filters without opting into every available transformation. Start there, observe logs and page behavior, and change rewrite policy deliberately rather than enabling every filter at once.

The cache directory must exist and be writable by the user running Nginx. Create it and apply ownership appropriate to your distribution, for example:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo mkdir -p /var/cache/ngx_pagespeed
sudo chown www-data:www-data /var/cache/ngx_pagespeed

On systems where Nginx runs as nginx rather than www-data, substitute that account and group. Check the effective user in your Nginx configuration before changing ownership. A read-only or inaccessible cache path is a common reason for failed optimization or repeated processing. Also verify available disk space and any mandatory access-control policy that could deny writes even when Unix ownership looks correct.

The three location blocks allow rewritten assets, PageSpeed’s static resources and its beacon endpoint to be served without being intercepted by an application’s normal routing. Keep them in the server configuration that handles the optimized site. If you use a more complex location hierarchy, confirm that these patterns are not shadowed by a higher-priority regular-expression or prefix location.

5. Test and reload safely

  1. Validate the complete configuration:
    sudo nginx -t
  2. Only after a successful test, reload workers without dropping existing connections:
    sudo systemctl reload nginx
  3. If the test reports an unknown directive, inspect the load_module line and verify that the shared-object path exists. If it reports a permission or cache error, fix the cache directory and rerun the test.

A reload does not repair a binary incompatibility. If Nginx refuses to start or reload after adding the module, restore the previous configuration or remove the new load line, then resolve the package or build mismatch before trying again.

6. Verify an optimized response

Request the response headers from a real HTTPS virtual host:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -sI https://yoursite/ | grep -i x-page-speed

A version value in X-Page-Speed indicates that the worker is active and processing the response. If no header appears, check these items in order:

  • The module is loaded exactly once in the main context.
  • pagespeed on; is in the http or applicable server context.
  • You queried the virtual host that contains the PageSpeed configuration.
  • The request reached Nginx rather than an upstream proxy or cache that stripped or replaced the header.
  • The cache directory is writable by the Nginx worker account.

Use a cache-busting query or a response that has not already been served by an intermediary when checking a first change. Header presence confirms module activity; it does not prove that every filter changed a particular page. Compare representative HTML and asset responses before and after enabling filters, and monitor your own cache behavior instead of relying on a universal percentage improvement.

Compile a dynamic module for custom Nginx

Choose this route for a custom Nginx build or a host outside the signed-package matrix. Build against the exact Nginx source version installed on the server. Dynamic modules are version-specific: repeat the build whenever Nginx is upgraded, even if the new release appears to be a minor update.

Build procedure

  1. Install a compiler toolchain, Nginx development dependencies and the maintained ngx_pagespeed source required by the module’s build guide. Keep the source tree for the exact Nginx release you operate. The build also needs the PageSpeed Optimization Library components expected by that maintained source; the package route is preferred because it supplies the prebuilt pieces.
  2. Change into that Nginx source tree. The documented example uses version 1.26.3 and a sibling module directory:
    cd ../nginx-1.26.3
    ./configure --add-dynamic-module=../ngx_pagespeed --with-compat
    make modules
  3. Copy the resulting shared object into Nginx’s module directory:
    sudo cp objs/ngx_pagespeed_module.so /usr/lib/nginx/modules/

    Use the module directory reported by your build if it differs.

  4. Add this line once, at the top level of nginx.conf:
    load_module modules/ngx_pagespeed_module.so;
  5. Add the same pagespeed directives and location blocks shown in the package method, then run sudo nginx -t.
  6. Reload with sudo systemctl reload nginx and verify X-Page-Speed with curl -sI.

The --with-compat option helps a dynamic module work across compatible builds, but it does not make an arbitrary module safe for every Nginx release or build configuration. Preserve the Nginx configure arguments and module source in configuration management. When an Nginx package update is planned, build and stage the replacement module first, test it, and only then restart or reload the service.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not copy a module compiled on one host to unrelated hosts merely because their Nginx version strings match. Compiler options, enabled features and distribution patches can differ. A module built against the wrong binary may produce an explicit “not binary compatible” error or fail before Nginx can serve traffic.

Migrate an existing open-source ngx_pagespeed installation

The maintained migration path is a package swap rather than a configuration rewrite:

  1. Back up nginx.conf and any included PageSpeed files.
  2. Remove the old module package or shared object according to how the existing installation was made.
  3. Install the signed nginx-module-pagespeed package for your supported distribution.
  4. Retain the existing pagespeed directives unless you have a separate reason to change filter behavior.
  5. Ensure exactly one load_module line refers to /usr/lib/nginx/modules/ngx_pagespeed_module.so (or the path installed on your system).
  6. Run sudo nginx -t, reload, and check the response header.

Do not leave both the legacy and maintained shared objects enabled. If Nginx reports a duplicate module or an undefined directive, inspect every file included by nginx.conf and remove the obsolete load line. Keep a copy of the old configuration until the replacement has passed a real request test, but do not load both implementations simultaneously.

Configuration placement and common failure modes

“unknown directive pagespeed”

Nginx parsed a pagespeed directive before loading the module, or the module was built for a different Nginx binary. Move load_module to the main context, verify the file path, and confirm that the package matches the running Nginx build.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

“module is not binary compatible”

The shared object was compiled against a different Nginx version or build options. Reinstall the distribution-matched package, or rebuild dynamically against the exact source and configuration of the Nginx binary you run. The --with-compat flag cannot correct an arbitrary mismatch.

Nginx starts, but no X-Page-Speed header appears

Check that the request reaches the configured server block, that pagespeed on; is active there, and that an upstream proxy is not removing the header. Confirm the module with nginx -V and inspect the effective configuration with your distribution’s included-file layout. Test the origin directly if a CDN or reverse proxy sits in front of Nginx.

Cache errors or no rewritten assets

Verify that /var/cache/ngx_pagespeed exists, is writable by the Nginx worker, and has sufficient disk space. Ensure the three PageSpeed locations are present and not overridden by another location rule. If the cache was created by a different user during testing, correct ownership before retesting.

Reload fails after an Nginx update

A source-built module may no longer match the new Nginx binary. Rebuild it against the new source version, install the replacement shared object, run nginx -t, and reload only after the test succeeds. Package installations are preferable on supported systems because the repository tracks the distribution’s Nginx build.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Package installs but the directive is still unavailable

Check whether the package installed a module file but your main configuration never includes its load file. Add one explicit load_module line in the main context, then test. Conversely, if a module-enabled include already loads it, remove the manual duplicate rather than adding another path.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Operational checklist

  • Use the maintained signed package on Ubuntu 22.04/24.04, Debian 11/12/13, supported EL9 systems, or EL10 x86_64 unless you have a concrete reason to compile.
  • Confirm that the package’s stock-Nginx assumption matches the binary actually running.
  • Keep load_module in the main context and PageSpeed directives in http or server contexts.
  • Start with RewriteLevel CoreFilters and a writable cache path.
  • Keep the three PageSpeed location rules with the virtual host that serves the optimized site.
  • Run sudo nginx -t before every reload.
  • Verify activation with the X-Page-Speed response header from the origin.
  • After every Nginx upgrade, rebuild a source-installed dynamic module before restarting the service.
  • Retain rollback copies of configuration and the prior module during migration.
  • Measure real page behavior and cache effects on your own site; no universal performance percentage is established by this installation guidance.

Frequently Asked Questions

Is ngx_pagespeed still maintained?

The original open-source ngx_pagespeed repository is archived. The maintained continuation is mod_pagespeed 2.1, which retains the familiar pagespeed directives and filter names. New deployments should use the maintained signed module rather than the legacy code, which has had no security patches since 2020.

Do I need to compile Nginx or PSOL?

Usually not. On a supported distribution, the signed nginx-module-pagespeed package is prebuilt and avoids a local PageSpeed Optimization Library build. Compile a dynamic module only for a custom Nginx binary or an operating-system and architecture combination outside the package matrix.

How much does ngx_pagespeed cost?

The module is free software. We-Amp lists optional support and hardened, attested builds as commercial offerings; contact the vendor for current terms and pricing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where does load_module belong?

Place load_module modules/ngx_pagespeed_module.so; once in Nginx’s main context, before events {} and http {}. It cannot be inside an http or server block. Debian-derived packages may load it automatically from modules-enabled.

How do I know PageSpeed is active?

Request an origin response with curl -sI https://yoursite/ | grep -i x-page-speed. A version value in X-Page-Speed indicates that the worker processed the response. If it is missing, check module loading, pagespeed on;, the selected virtual host, cache permissions and any proxy that may strip the header.

What cache permissions are required?

The directory named by pagespeed FileCachePath must exist and be writable by the Nginx worker account. Use www-data on many Debian or Ubuntu systems and nginx on many Enterprise Linux systems, but verify the user in your own configuration before changing ownership.

Can I use the package with Nginx from another repository?

Not automatically. The package is built for the distribution’s stock Nginx ABI. A third-party or custom Nginx binary can be incompatible even with the same version number, so build the module against that binary’s exact source and configuration unless compatibility is explicitly confirmed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What must I do after upgrading Nginx?

A source-installed dynamic module is version-specific. Rebuild it against the new Nginx source, copy the replacement shared object into the module directory, run nginx -t and reload only after the test succeeds. Supported package installations normally follow the distribution’s compatible Nginx updates.

Can I leave the old and maintained modules enabled during migration?

No. Remove or disable the legacy shared object and ensure exactly one load_module line names the maintained module. Loading both can cause duplicate-module errors or undefined directives.

Does X-Page-Speed prove my pages are faster?

No. It confirms that the module is loaded and processing a response. Measure representative pages, assets, cache hits and origin behavior on your own site; no dated primary benchmark establishes a universal improvement percentage for this installation.

The Bottom Line

On a supported Ubuntu, Debian or Enterprise Linux host, install the signed nginx-module-pagespeed package, load it once in Nginx’s main context, enable CoreFilters with a writable cache, test and reload, then confirm X-Page-Speed. Compile only for custom or unsupported Nginx builds, and rebuild the dynamic module whenever Nginx changes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Linux administrationmod_pagespeedNGINXngx_pagespeedweb performance

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.