最後更新: 2017-06-14
介紹
HomePage: https://developers.google.com/speed/pagespeed/module/
下載及安裝
# Link
https://modpagespeed.com/doc/download
# Debian (6~7M)
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_a...
dpkg -i mod-pagespeed-*.deb
dpkg -l | grep pagespeed
ii mod-pagespeed-stable 1.11.33.2-r0 amd64
# Centos (6~7M)
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x...
rpm -Uvh mod-pagespeed-stable_current_x86_64.rpm
安裝 package 後獲得以下 File
Debian
Bin
- /usr/lib/apache2/modules/mod_pagespeed_ap24.so
- /usr/bin/pagespeed_js_minify
Configure
- /etc/apache2/mods-available/pagespeed.conf
- /etc/apache2/mods-available/pagespeed.load
- /etc/apache2/conf.d/pagespeed_libraries.conf
- /etc/cron.daily/mod-pagespeed
Centos
/etc/httpd/conf.d/pagespeed_libraries.conf
應用
service apache2 restart
我的設定
pagespeed.conf
ModPagespeed off # global configuration propagating to VHosts ModPagespeedInheritVHostConfig on # Direct Apache to send all HTML output to the mod_pagespeed output handler. AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html # directory must exist and be writable by the apache user ModPagespeedFileCachePath "/var/cache/mod_pagespeed/" # statistics log ModPagespeedLogDir "/var/log/pagespeed" # PassThrough, CoreFilters, and OptimizeForBandwidth # 用 "PassThrough" 之後再個別 enable filter one by one ModPagespeedRewriteLevel PassThrough # enable filter one by one ModPagespeedEnableFilters combine_css,combine_javascript ModPagespeedEnableFilters collapse_whitespace,elide_attributes,remove_quotes,trim_urls,remove_comments ModPagespeedEnableFilters extend_cache ModPagespeedEnableFilters rewrite_css,rewrite_images # Other Setting ModPagespeedCombineAcrossPaths off ....
vhosts.conf
<VirtualHost *:80>
# Enable Pagespeed
ModPagespeed on
....
</VirtualHost>
ImageMaxRewritesAtOnce
This option sets the maximum number of images to optimize concurrently. The default value is 8.
To allow optimizing an unlimited number of images concurrently, set this option to "-1".
To stop optimizing images, instead disable recompress_images or the related filters.
Admin Page
Apache Handler: pagespeed_admin
Covers all administrative functions for a host in one handler.
If you establish this handler, you don't need any of the other server-scoped methods.
<Location /pagespeed_admin> Order allow,deny Allow from 127.0.0.1 SetHandler pagespeed_admin </Location>
Apache's SetHandler access controls are accessible to anyone who can modify .htaccess files
so in a typical shared hosting context the global admin site isn't sufficiently protected.
PageSpeed allows setting an additional restriction of what domains are allowed to load handlers.
ModPagespeedStatisticsDomains Disallow * ModPagespeedGlobalStatisticsDomains Disallow * ModPagespeedMessagesDomains Disallow * ModPagespeedConsoleDomains Disallow * ModPagespeedAdminDomains Disallow * ModPagespeedGlobalAdminDomains Disallow *
If you explicitly Allow access to any site, all others are automatically Disallowed.
ModPagespeedStatisticsDomains Allow admin.example.com ModPagespeedGlobalStatisticsDomains Allow admin.example.com ModPagespeedMessagesDomains Allow admin.example.com ModPagespeedConsoleDomains Allow admin.example.com ModPagespeedAdminDomains Allow admin.example.com ModPagespeedGlobalAdminDomains Allow admin.example.com
#### Global Setting
# shared memory to allocate as a circular buffer for holding recent PageSpeed log messages
# Unit: byte
ModPagespeedMessageBufferSize 1048576
Cache setting
# 當超過此時間, pagespeed 就會發放原本(沒有優化)的檔案
# 0 相一直等
ModPagespeedRewriteDeadlinePerFlushMs 0
# metadata will no longer be written to the filesystem cache
# Unlike the LRU cache, this cache is shared among all server processes
# The first is the exact string given as the argument to FileCachePath
# in any virtual host where you want this cache active.
#
# information will be lost upon server restart. (kbyte)
# PageSpeed checkpoints the shared memory metadata cache to disk,
# providing almost all the performance but
# without the cache being wiped on restart
ModPagespeedCreateSharedMemoryMetadataCache "/var/cache/mod_pagespeed/" 51200 ModPagespeedShmMetadataCacheCheckpointIntervalSec 300
Any virtual host that does not have a shared memory metadata cache configured with CreateSharedMemoryMetadataCache will share a default one.
snapshots of the shared memory cache are periodically saved to disk via shared memory checkpointing.
# File Cache (built-in LRU mechanism to remove old files) ModPagespeedFileCachePath "/var/cache/pagespeed/" ModPagespeedFileCacheSizeKb 102400 ModPagespeedFileCacheCleanIntervalMs 3600000 ModPagespeedFileCacheInodeLimit 500000
ModPagespeedDisallow
要放在再頂的 .htaccess
ModPagespeedDisallow "*/plugins/akismet/*"
These directives are evaluated in sequence for each resource,
to determine whether the resource should be consider for rewriting.
- Excluding JavaScript files that cannot be rewritten
(Some JavaScript files are sensitive to their own names as they traverse the DOM)
# Specifying explicitly which types of files can be rewritten
ModPagespeedDisallow "*" ModPagespeedAllow "http://*example.com/*.html" ModPagespeedAllow "http://*example.com/*/images/*.png" ModPagespeedAllow "http://*example.com/*/styles/*.css" ModPagespeedDisallow "*/images/captcha/*"
# Limiting the maximum size of a resource to optimize (Default: 16M)
ModPagespeedMaxCacheableContentLength 16777216
# Limiting the maximum generated URL segment length (Default: 1024)
# Apache servers by default impose a further limitation of about 250 characters
# per URL segment (text between slashes)
ModPagespeedMaxSegmentLength 250
dedup_inlined_images
# dedup_inlined_images replaces repeated inlined images with
# JavaScript that loads the image from the first occurence of the image.
ModPagespeedEnableFilters dedup_inlined_images
local_storage_cache
The filter determines which resources are in local storage by inspecting a browser cookie set by the last step's onload handler.
The cookie's name is _GPSLSC and its value is a list of hashes of the URLs of resources stored in local storage.
# Requirements filters: inline_css, inline_javascript ModPagespeedEnableFilters local_storage_cache ModPagespeedEnableFilters inline_css ModPagespeedEnableFilters inline_javascript
Example
https://modpagespeed.com