Pagespeed module

Pagespeed module

Install ngx_pagespeed

Build-script for nginx pagespeed module on debian: pitchup/ngx_pagespeed_deb

Enable Google Pagespeed

In every server block where PageSpeed is enabled add, but this is configuration for HTTPS:

echo '

# Turn on ngx_pagespeed
pagespeed on;

# Use memcached
pagespeed MemcachedThreads 1;
pagespeed MemcachedServers "localhost:11211";

# using shared mem, writable by nginx daemon
# directive is mandatory, even if not needed with memcached
pagespeed FileCachePath /dev/shm/ngx_pagespeed_cache;
# OR: pagespeed FileCachePath /var/www/;

# filter settings:
pagespeed RewriteLevel CoreFilters;
pagespeed EnableFilters collapse_whitespace,remove_comments;

# pagespeed RespectVary on;
pagespeed DisableRewriteOnNoTransform off;

# preserve url relativity
pagespeed PreserveUrlRelativity on;

# Mandatory for SSL
# Avoid HTTP transport altogether
pagespeed LoadFromFile ""  "/var/www/vhosts/";
pagespeed LoadFromFile "" "/var/www/vhosts/";

# Req for pagespeed optimized resources go to pagespeed handler & no extra headers get set
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

' > /var/www/vhosts/

Make sure that this is included:

echo '

# additional directives accessible via ftp
include /var/www/vhosts/*.conf;

' >> /var/www/vhosts/system/

Some pagespeed-rules

Check config with service nginx configtest, reload service nginx reload and test if ngx_pagespeed is active:

curl -s -I | grep -i "X-Page-Speed"

Configure ngx_pagespeed

Problem with temp-cache path in shared memory

First of all, if using memcached I don’t think this is important anymore.

The cache directory for FileCachePath directive must already created and writeable by the Nginx daemon.

I can fix that with:

echo '
# fix-script executed from /etc/rc.local
mkdir -p /run/shm/ngx_pagespeed_cache/
chown www-data:www-data /run/shm/ngx_pagespeed_cache/
' > /usr/local/bin/

# make it executable
chmod +x /usr/local/bin/

TMP='[ -x \/usr\/local\/bin\/ ] \&\& \/usr\/local\/bin\/'
sed -i -re "s/^(exit 0)/$TMP\n\1/g" /etc/rc.local

Configuring PageSpeed Filters Using ngx_pagespeed With nginx On Debian Wheezy

SSL support

There is an additional configuration directives needed to make ngx_pagespeed work properly with SSL.

How To Setup Nginx & Pagespeed with SSL

Instead MapOriginDomain we will use LoadFromFile which is the same thing except it uses the file-system instead of the network to retrieve the resource.


Purge / flush pagespeed cache:*

touch /var/www/

The system may take up to 5 seconds to take effect. This does not delete the old files but it tells PageSpeed to ignore those files.

Original cache lifetime of the resource is by default 5 minutes. So, whenever you change something on static files, there can be a lag until it’s seen online.

The file cache is always required since files larger than 1Mb cannot be stored on memcached (which is, by the way, not true), and it is important to optimize large images.

These are all filters that are not included in CoreFilters set:

pagespeed UseExperimentalJsMinifier on;
pagespeed EnableFilters include_js_source_maps;

pagespeed EnableFilters prioritize_critical_css;
pagespeed EnableFilters move_css_above_scripts, move_css_to_head;
pagespeed EnableFilters sprite_images;
pagespeed EnableFilters inline_google_font_css;
pagespeed EnableFilters convert_to_webp_lossless;
pagespeed EnableFilters insert_image_dimensions;
pagespeed EnableFilters inline_preview_images,resize_mobile_images;
pagespeed EnableFilters dedup_inlined_images;
pagespeed EnableFilters remove_comments,collapse_whitespace;
pagespeed EnableFilters insert_dns_prefetch;

# High risk
pagespeed EnableFilters defer_javascript;

Interesting filters:

Canonicalize JavaScript Libraries  |  PageSpeed Module  |  Google Developers

danriti/bootstrap-pagespeed · GitHub Perfect PageSpeed score with the help of mod_pagespeed - automation ftw! · Issue #4 · danriti/bootstrap-pagespeed · GitHub

Prioritize critical CSS and defer JavaScript are the two “workhorse” filters (just as you noted in your writeup), but they are disabled by default.

For critical CSS the server beacons back the “above the fold” CSS, so keep in mind that you’ll have to reload the page a few times before it begins to inline resources.

Nginx performance tips using ngx_pagespeed - MaxCDN Blog

Purging Pagespeed cache

Flush it

We can always flush: but we first must set something:

# Enable URL cache purging
pagespeed EnableCachePurge on;
pagespeed PurgeMethod PURGE;

# Enable admin for HTTP cache purging. Changed "pagespeed_admin" to "_pagespeed" for basic security

pagespeed AdminPath /_pagespeed;
location /_pagespeed { allow all; }

And then flush with:

curl --request PURGE '*'

but this HTTP request should work also:*

Or we can create a small PHP file like here:

Shorten TTL (not a viable solution)

We can shorten origin TTL update from a lot to say 10 min.

The best solution is this

Pagespeed module works after processing by HTTP server, but watch out - this will change that for specified directory (static files).

Also, because PageSpeed is loading the files directly from the filesystem, no custom headers will be set. For example, no headers set with the Header set (Apache) or add_header (Nginx) directives will be applied to these resources.

Loading static files from disk

pagespeed LoadFromFile "" "/var/www/static/";


pagespeed LoadFromFileMatch "^https?://[^/]*)/static/" "/var/www/static/\\1";

And we CAN use (As of script variables on nginx:

pagespeed LoadFromFile "http://$host/static" "$document_root/static";

This should only be used for completely static resources which do not need any custom headers or special server processing.

Real solution

I have that, but it didn’t pick up because it is set for ganzohr, and not for other domains. pagespeed LoadFromFile “” “{DOCROOT}/";

Huge everything, if needed:

pagespeed StatisticsPath /ngx_pagespeed_statistics;
pagespeed GlobalStatisticsPath /ngx_pagespeed_global_statistics;
pagespeed MessagesPath /ngx_pagespeed_message;
pagespeed ConsolePath /pagespeed_console;
pagespeed AdminPath /pagespeed_admin;
pagespeed GlobalAdminPath /pagespeed_global_admin;

location /ngx_pagespeed_statistics { allow all; }
location /ngx_pagespeed_message    { allow all; }
location /pagespeed_admin          { allow all; }
location /pagespeed_console        { allow all; }

touch /var/ngx_pagespeed_cache/cache.flush

This must be done from command line though. Also, keep in mind that this won’t remove the files form cache but, rather tell pagespeed not to consider them until the new versions of the files have replaced the old ones.

mkdir -p /var/ngx_pagespeed_cache chown www-data:www-data /var/ngx_pagespeed_cache mount -t tmpfs -o size=200M,mode=0755 tmpfs /var/ngx_pagespeed_cache

echo ‘# Temporary space for Google Pagespeed module’ » /etc/fstab echo ‘tmpfs /var/ngx_pagespeed_cache tmpfs size=200M,mode=0755 0 0’ » /etc/fstab

Source: Nginx PageSpeed configuration - GetPageSpeed

PageSpeed System Integration  |  PageSpeed Module  |  Google Developers no pagespeed_admin - Google Search PageSpeed Console  |  PageSpeed Module  |  Google Developers PageSpeed Admin Pages  |  PageSpeed Module  |  Google Developers Seite nicht gefunden - Using Pagespeed - EasyEngine Install Google mod_pagespeed - The place of all tech articles nginx “pagespeed_admin” laravel - Google Search PageSpeed Console  |  PageSpeed Module  |  Google Developers New Tab

How I achieved 100/100 pagespeed score?

  • Google Pagespeed module used extensively
  • nginx: gzip, but that can be skipped as pagespeed would do that if i have not
  • nginx: browser caching, but also not sure if google pagespeed can fix that also
  • Google Webfont Optimizer to load fonts using official Web Font Loader technique.
  • W3 Total Cache: used for nothing, except for simple page cache to speed up server processing
  • Used Busted! plugin to have bust a cache automatically.
  • I have temporarily disabled analytics, but that can be solved also by loading scripts locally. There is a plugin for that, also.

I have a problem of FOUT because of fonts. Well there is a huge discussion on it: FOUT, FOIT, FOFT

I have experimented with the following tools:

  • Plugin: Above The Fold Optimization The best feature is in moving the critical path CSS to inline and load CSS asynchronously with filamentgroup/loadCSS library.

    Anyway, loading CSS as it should is done good enough by pagespeed module.

    If you want to async load Google fonts, you also must to install more plugins.

    To have local Analytics scripts you must also install Autoptimize plugin and enabled Optimize HTML Code? in it. This also works with DuracellTomi’s Google Tag Manager plugin.

    Note: I tried to localize analytics scripts using Pagespeed module, but that was unsuccessful as the scripts are invoked at runtime

Above The Fold Optimization — WordPress Plugins

Above The Fold Optimization I use it as a LoadCSS.

Similar: Defer CSS Addon for BWP Minify — WordPress Plugins

addyosmani/critical: Extract & Inline Critical-path CSS in HTML pages filamentgroup/criticalCSS: Finds the Above the Fold CSS for your page, and outputs it into a file

I think this is the most complete article on pagespeed optimization is this: Complete Guide: How to Speed Up WordPress

And this also: Google PageSpeed Insights - Scoring 100/100 with WordPress

  • pagespeed AvoidRenamingIntrospectiveJavascript off (probaj tako da keširaš analitiku) Radiće i analitika!


Couple of visits.

pagespeed CriticalImagesBeaconEnabled false;

Filters using beacons are: lazyload_images, inline_preview_images, inline_images and prioritize_critical_css as they need to determine which images are above the fold or which CSS is actually used by the page.

Controlling the use of beacons Why are rewritten pages sending POSTs back to my server?

If beacon are disabled, lazyload_images will lazily load all images.

prioritize_critical_css injects JavaScript that uses a beacon to report back to the server the CSS that is used by the page. It takes at least two visits to instrument the page, collect the appropriate beacon data, and extract the required CSS before a fully optimized page is presented; until then all CSS is optimized as normal: minified, combined, etc.

Pagespeed Cache

GooglePageSpeed - Learn how to configure Google’s mod_pagespeed Apache module on Ubuntu 14.10

PageSpeed is designed to work without any caches between it and the user.

PageSpeed System Integration  |  PageSpeed Module  |  Google Developers

The file cache is always required since files larger than 1Mb cannot be stored on memcached, and it is important to optimize large images.

Finally, a per-process in-memory LRU cache and/or an interprocess shared-memory cache can be configured for rapid access to small objects.

Can I move PageSpeed’s file-based cache into RAM?


It is obvious that memcached is not the fastest solution.

Also it would be really perfect to make Nginx’s cache, not memcached. Memcached sucks, it slow’s down’s the whole structure and if i’ll add ngx_pagespeed + memcached to my server - i’ll see slow down and i’ll have no choice expect to remove ngx_pagespeed and memcached.

In-memory LRU Cache

To optimize performance, a small in-memory write-through LRU cache can be instantiated in each server process. Nginx typically runs with many fewer processes, so a larger LRUCacheKbPerProcess is appropriate there. In Kb

pagespeed LRUCacheKbPerProcess     8192;
pagespeed LRUCacheByteLimit        16384;

In-memory LRU Cache

All our caches evict the entry that has gone the longest without access, which is a “Least Recently Used” policy.

Shared Memory Metadata Cache

What is Pagespeed metadata? Pagespeed stores info on how to apply optimizations to web pages - metadata entries. They are small but frequently accessed.


Shared Memory Metadata Cache

If not specified, there is fallback, default shared memory metadata cache that is always enabled by default.

Native URL fetcher

Pagespeed uses the serf fetcher by default, but it also has an experimental fetcher that avoids the need for a separate thread by using native Nginx events. In initial testing this fetcher is about 10% faster.

Tuning Threading

If you don’t want to use a disk cache with ngx_pagespeed there are currently two ways to do that:

  1. memcached
  2. file cache on tmpfs (ramdisk)

If you’re only on a single server then (2) is probably best; if you want to share your cache between servers then you need (1).

Change Beacon URL as I don’t want pagespeed signature

BeaconUrl (bu) /ngx_pagespeed_beacon

Maybe disable beacons?

How do I enable or disable beacon POSTs being sent back to my server?

No need for noscript support Frequently Asked Questions  |  PageSpeed Module  |  Google Developers

pagespeed SupportNoScriptEnabled false;

Important explanation: PageSpeed does not pick up changes when I edit CSS or JavaScript files

Can’t rewrite gzipped resources

This is no longer needed; we fixed it about a year ago.

Check with:

cat <<'EOF' >> /etc/nginx/fastcgi_params

# Turn PHP compression off, so pagespeed can read it properly
fastcgi_param HTTP_ACCEPT_ENCODING "invalid";


Again, the above is not needed anymore.

In-Place Resource Optimization

pagespeed InPlaceResourceOptimization on;

Important! Tags can only be combined when no information will get lost.

Debugging pagespeed <!--deadline_exceeded--> You can try that in Screaming Frog or increase the value.

Also: <!--Cannot rewrite

Limiting the number of concurrent image optimizations, default 8 No need to change as no more cores.

Fetch external resources with Pagespeed module

PageSpeed Authorizing and Mapping Domains

pagespeed MapProxyDomain target_domain/subdir origin_domain/subdir [rewrite_domain/subdir];

pagespeed MapProxyDomain

# Localize analytics only with pagespeed
pagespeed MapProxyDomain   
pagespeed MapProxyDomain

It’s beautiful! I don’t need no cache-buster!

Set the cache lifetime of css, js, and images to the maximum and insert a hash of their contents into the url.

It’s on by default: extend_cache

With this in place, PageSpeed is able to optimize the script and shave off a few more bytes next to cache-extend the file to a one year TTL according to confuration while still being able to respect the original cache TTLs and thereby force updates through to visitors at the desired frequencies.

pagespeed MapProxyDomain


Canonicalize JavaScript Libraries

date 01. Jan 0001 | modified 28. May 2021
filename: Server - ( Nginx ) Pagespeed module