---
title: "PageSpeed Module Alternatives for Nginx and Apache"
description: "mod_pagespeed and ngx_pagespeed are retired. Get the same wins on Ubuntu 24.04 with Brotli, gzip, cache headers, minified assets and WebP in Nginx or Apache."
category: "Web Performance"
language: en
source: https://cubepath.com/docs/web-performance/pagespeed-module-nginx-apache
updated: 2026-09-25
---
# PageSpeed Module Alternatives for Nginx and Apache
Google's PageSpeed modules, `mod_pagespeed` for Apache and `ngx_pagespeed` for Nginx, rewrote pages on the fly to compress, minify and convert assets. The project was donated to the Apache Software Foundation and later retired: it receives no updates, has no packages for Ubuntu 24.04 and building it against a current Nginx is not supported. This tutorial explains what the modules did and shows how to get the same results on Ubuntu 24.04 with maintained tools: Brotli and gzip compression, long-lived cache headers, minification at build time and WebP images, for both Nginx and Apache.
## What PageSpeed did and what replaces it
Most PageSpeed filters map to a feature that the web server or your build process now handles better, because the work is done once instead of on every request:
| PageSpeed filter | Modern replacement |
|------------------|--------------------|
| `rewrite_css`, `rewrite_javascript` (minify) | Minify at build or deploy time with esbuild |
| `combine_css`, `combine_javascript` | HTTP/2 multiplexing, or bundling at build time |
| gzip handling | gzip plus Brotli in the web server |
| `extend_cache` | `Cache-Control` headers with fingerprinted file names |
| `convert_jpeg_to_webp`, `recompress_images` | Pre-generated WebP/AVIF served by `Accept` header |
| `lazyload_images` | Native `loading="lazy"` attribute in HTML |
| `defer_javascript` | `defer` attribute on `
```
Optionally, pre-compress the minified files at the highest level so the `brotli_static` and `gzip_static` directives from Step 1 serve them without spending CPU per request (Nginx only):
```bash
sudo apt install brotli
brotli --best --keep css/style.min.css js/app.min.js
gzip --best --keep css/style.min.css js/app.min.js
```
This creates `.br` and `.gz` files beside each asset. Nginx serves them automatically when the client accepts that encoding. Regenerate them on every deploy, because a stale `.br` file is served instead of the updated original.
## Step 4 - Serving WebP images and lazy loading
Image conversion was PageSpeed's biggest win. Do it once with `cwebp` and let the server pick the format based on the browser's `Accept` header.
Install the WebP tools and convert your images, keeping the original extension in the new name (`photo.jpg` becomes `photo.jpg.webp`):
```bash
sudo apt install webp
cd /var/www/your_domain
find images -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec sh -c 'cwebp -quiet -q 80 "$1" -o "$1.webp"' _ {} \;
```
On Nginx, add a `map` to the `http` context so the server knows whether the client accepts WebP:
```bash
sudo nano /etc/nginx/conf.d/webp.conf
```
```nginx
map $http_accept $webp_suffix {
default "";
"~*image/webp" ".webp";
}
```
Then, in your site's server block, place this location before the generic static-asset location from Step 2 so it matches first:
```nginx
location ~* \.(?:jpe?g|png)$ {
add_header Vary Accept;
expires 1y;
try_files $uri$webp_suffix $uri =404;
}
```
Reload and check that a browser advertising WebP receives it:
```bash
sudo nginx -t && sudo systemctl reload nginx
curl -s -o /dev/null -D - -H 'Accept: image/webp,*/*' https://your_domain/images/photo.jpg | grep -i content-type
```
```output
content-type: image/webp
```
For Apache, and for AVIF support and automated conversion of new uploads, see the image optimization pipeline guide, which covers this in depth.
Finally, replace PageSpeed's `lazyload_images` with the native attribute on images below the fold. Keep the first visible image (the hero or logo) without it so it loads immediately:
```html
```
Setting `width` and `height` lets the browser reserve space and avoids layout shifts.
## Step 5 - Measuring the result
Run Lighthouse from Chrome DevTools (the **Lighthouse** tab) or on pagespeed.web.dev before and after these changes. Focus on Largest Contentful Paint and total transfer size. From the server you can compare transfer sizes directly:
```bash
curl -s -o /dev/null -w '%{size_download} bytes\n' https://your_domain/css/style.css
curl -s -o /dev/null -w '%{size_download} bytes\n' -H 'Accept-Encoding: br' https://your_domain/css/style.css
```
```output
48213 bytes
9120 bytes
```
## Troubleshooting
**`nginx -t` reports `unknown directive "brotli"`.** The module packages are not installed or their files in `/etc/nginx/modules-enabled/` were removed. Reinstall `libnginx-mod-http-brotli-filter`.
**`nginx -t` reports `"gzip" directive is duplicate`.** The `gzip on;` line in `/etc/nginx/nginx.conf` is still active. Comment it out as shown in Step 1.
**Responses are not compressed although the configuration is correct.** Check the file's `Content-Type` with `curl -I`. If it is not in `brotli_types` or `AddOutputFilterByType`, it is skipped. Responses behind a CDN may also be recompressed or decompressed by the CDN.
**Visitors see old CSS after a deploy.** A file with a one-year `immutable` cache was changed without renaming it. Use fingerprinted file names or add a version query string such as `style.min.css?v=2` in the HTML.
## Conclusion
You replaced every useful PageSpeed filter with a maintained alternative: Brotli and gzip compression in the server, long-lived cache headers, minification and pre-compression at deploy time, WebP images chosen by `Accept` header and native lazy loading. These changes run once per deploy instead of on every request, so they cost less CPU than PageSpeed did. Next, consider enabling HTTP/3, adding AVIF images and putting a CDN in front of your static assets.