Skip to content

Instantly share code, notes, and snippets.

@lbensaad
Created April 20, 2015 05:37
Show Gist options
  • Save lbensaad/505e5bf840a9d09143d3 to your computer and use it in GitHub Desktop.
Save lbensaad/505e5bf840a9d09143d3 to your computer and use it in GitHub Desktop.
ngx_pagespeed downstream caching config file
server {
listen 80;
server_name wp;
pagespeed off;
server_tokens off;
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't cache uris for wp social login
if ($request_uri ~* "/wp-content/plugins/wordpress-social-login/hybridauth/?") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
# ******** https://developers.google.com/speed/pagespeed/module/downstream-caching ********
# Block 2: Define prefix for proxy_cache_key based on the UserAgent.
# Define placeholder PS-CapabilityList header values for large and small
# screens with no UA dependent optimizations. Note that these placeholder
# values should not contain any of ll, ii, dj, jw or ws, since these
# codes will end up representing optimizations to be supported for the
# request.
set $default_ps_capability_list_for_large_screens "LargeScreen.SkipUADependentOptimizations";
set $default_ps_capability_list_for_small_screens "TinyScreen.SkipUADependentOptimizations";
# As a fallback, the PS-CapabilityList header that is sent to the upstream
# PageSpeed server should be for a large screen device with no browser
# specific optimizations.
set $ps_capability_list $default_ps_capability_list_for_large_screens;
# Cache-fragment 1: Desktop User-Agents that support lazyload_images (ll),
# inline_images (ii) and defer_javascript (dj).
# Note: Wget is added for testing purposes only.
if ($http_user_agent ~* "Chrome/|Firefox/|Gecko|Trident/6\.|Safari|Wget") {
set $ps_capability_list "ll,ii,dj:";
}
# Cache-fragment 2: Desktop User-Agents that support lazyload_images (ll),
# inline_images (ii), defer_javascript (dj), webp (jw) and lossless_webp
# (ws).
if ($http_user_agent ~*
"Chrome/[2][3-9]+\.|Chrome/[[3-9][0-9]+\.|Chrome/[0-9]{3,}\.") {
set $ps_capability_list "ll,ii,dj,jw,ws:";
}
# Cache-fragment 3: This fragment contains (a) Desktop User-Agents that
# match fragments 1 or 2 but should not because they represent older
# versions of certain browsers or bots and (b) Tablet User-Agents that
# correspond to large screens. These will only get optimizations that work
# on all browsers and use image compression qualities applicable to large
# screens. Note that even Tablets that are capable of supporting inline or
# webp images, e.g. Android 4.1.2, will not get these advanced
# optimizations.
if ($http_user_agent ~* "Firefox/[1-2]\.|bot|Yahoo!|Ruby|RPT-HTTPClient|(Google \(\+https\:\/\/developers\.google\.com\/\+\/web\/snippet\/\))|Android|iPad|TouchPad|Silk-Accelerated|Kindle Fire") {
set $ps_capability_list $default_ps_capability_list_for_large_screens;
}
# Cache-fragment 4: Mobiles and small screen Tablets will use image compression
# qualities applicable to small screens, but all other optimizations will be
# those that work on all browsers.
if ($http_user_agent ~* "Mozilla.*Android.*Mobile*|iPhone|BlackBerry|Opera Mobi|Opera Mini|SymbianOS|UP.Browser|J-PHONE|Profile/MIDP|portalmmm|DoCoMo|Obigo|Galaxy Nexus|GT-I9300|GT-N7100|HTC One|Nexus [4|7|S]|Xoom|XT907") {
set $ps_capability_list $default_ps_capability_list_for_small_screens;
}
# Block 3a: Bypass the cache for .pagespeed. resource. PageSpeed has its own
# cache for these, and these could bloat up the caching layer.
if ($uri ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+") {
set $skip_cache 1;
}
# Block 3b: Only cache responses to clients that support gzip. Most clients
# do, and the cache holds much more if it stores gzipped responses.
if ($http_accept_encoding !~* gzip) {
set $skip_cache 1;
}
# Block 4: Location block for purge requests.
location ~ /purge(/.*) {
# allow localhost;
allow 127.0.0.1;
# allow YOUR-SERVER-IP;
deny all;
proxy_cache_purge proxycache $ps_capability_list$1$is_args$args;
}
# Block 6: Location block with proxy_cache directives.
location / {
# 1: Upstream PageSpeed server is running at localhost:8050.
proxy_pass http://127.0.0.1:8080;
# 2: Use htmlcache as the zone for caching.
proxy_cache proxycache;
# 3: Bypass requests that correspond to .pagespeed. resources
# or clients that do not support gzip etc.
proxy_cache_bypass $skip_cache;
# 4: Use the redefined proxy_cache_key and make sure the /purge/
# block uses the same key.
proxy_cache_key $ps_capability_list$uri$is_args$args;
# 5: Forward Host header to upstream server.
proxy_set_header Host $host;
# 6: Set the PS-CapabilityList header for PageSpeed server to respect.
proxy_set_header PS-CapabilityList $ps_capability_list;
# 7: Add a header for identifying cache hits/misses/expires. This is
# for debugging purposes only and can be commented out in production.
add_header X-Cache $upstream_cache_status;
# Block 5b: Override Cache-Control headers as needed.
# Hide the upstream cache control header.
proxy_hide_header Cache-Control;
# Add the inferred Cache-Control header.
add_header Cache-Control $new_cache_control_header_val;
#********* Other settings
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
proxy_ignore_headers "Set-Cookie";
}
# **********************************************
}
server {
listen 8080;
server_name wp;
root /var/www/wp;
index index.php index.html index.htm;
server_tokens off;
pagespeed on;
location /{
pagespeed DownstreamCachePurgeLocationPrefix http://localhost:80;
pagespeed DownstreamCachePurgeMethod GET;
pagespeed DownstreamCacheRewrittenPercentageThreshold 95;
try_files $uri $uri/ /index.php?$args;
}
pagespeed Domain http://wp;
pagespeed LoadFromFile "http://wp/" "/var/www/wp/";
pagespeed FileCachePath /var/run/ngx_pagespeed_cache;
pagespeed RewriteLevel PassThrough;
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters canonicalize_javascript_libraries;
pagespeed EnableFilters combine_css;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters elide_attributes;
pagespeed EnableFilters extend_cache;
pagespeed EnableFilters flatten_css_imports;
pagespeed CssFlattenMaxBytes 5120;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters insert_dns_prefetch;
pagespeed EnableFilters prioritize_critical_css;
pagespeed EnableFilters defer_javascript;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
# add_header Cache-Control "public, max-age=600";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment