Skip to content

Instantly share code, notes, and snippets.

@rahul286
Last active January 10, 2023 18:28
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rahul286/22f5a0d8b3a6617bc240 to your computer and use it in GitHub Desktop.
Save rahul286/22f5a0d8b3a6617bc240 to your computer and use it in GitHub Desktop.
nginx + pagespeed downstream caching example
#source: http://www.jefftk.com/2014-01-03--nginx.conf
# for debugging with valgrind
#daemon off;
#master_process off;
#user nobody;
worker_processes 1;
worker_rlimit_core 500M;
working_directory /tmp/nginx_cores/;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log debug;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format withcookie '$remote_addr - $remote_user [$time_local] '
'"$request" (fvt=$cookie_fvt) $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
map_hash_max_size 65536;
include mime.types;
#default_type application/octet-stream;
default_type text/html;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=htmlcache:10m;
# When combined with proxy_hide_header and 'add_header Cache-Control'
# below, this mapping allows us to override html caching headers while
# leaving them alone on other types of content.
map $upstream_http_content_type $new_cache_control_header_val {
default $upstream_http_cache_control;
"~*text/html" "no-cache, max-age=0";
}
map $uri $new_post_url {
default no-redirect;
/news/index /p/index;
/news/all /p/index;
include /usr/local/nginx/conf/nginx.redirects.conf;
}
# enable gzip:
gzip on;
gzip_vary on;
# Turn on gzip for all content types that should benefit from it.
gzip_types application/ecmascript;
gzip_types application/javascript;
gzip_types application/json;
gzip_types application/pdf;
gzip_types application/postscript;
gzip_types application/x-javascript;
gzip_types image/svg+xml;
gzip_types text/css;
gzip_types text/csv;
# "gzip_types text/html" is assumed.
gzip_types text/javascript;
gzip_types text/plain;
gzip_types text/xml;
gzip_http_version 1.0;
server {
listen 80;
server_name bidadance.org;
rewrite ^(.*) http://www.bidadance.org permanent;
}
server {
listen 80;
server_name jefftk.com;
rewrite ^(.*) http://www.jefftk.com$1 permanent;
}
server {
listen 80;
server_name trycontra.com;
rewrite ^(.*) http://www.trycontra.com$1 permanent;
}
server {
if ($new_post_url !~ "no-redirect") {
rewrite ^ $new_post_url permanent;
}
# This server represents the external caching layer server which
# receives user requests and proxies them to the upstream server
# running on 8090 when the response is not available in
# the cache. It also services purge requests from the upstream server.
listen 80;
server_name www.jefftk.com;
access_log logs/access.80.log withcookie;
#pagespeed FileCachePath /var/ngx_pagespeed_cache;
#pagespeed off;
#normalize all accept-encoding headers to just gzip
set $gzip_support "0";
if ($http_accept_encoding ~* gzip) {
set $gzip_support "1";
}
# Block 2: Define prefix for proxy_cache_key based on the UserAgent.
set $ua_dependent_ps_capability_list "";
set $bypass_cache 1;
if ($http_user_agent ~* "Chrome/|Firefox/|MSIE |Safari|Wget") {
# User Agents that support lazyload-images (ll), inline-images (ii) and
# defer-javascript (dj).
set $ua_dependent_ps_capability_list "ll,ii,dj:";
set $bypass_cache 0;
}
if ($http_user_agent ~*
"Chrome/[2][3-9]+\.|Chrome/[[3-9][0-9]+\.|Chrome/[0-9]{3,}\.") {
# User Agents that support lazyload-images (ll), inline-images (ii),
# defer-javascript (dj), webp (jw) and webp-lossless (ws).
set $ua_dependent_ps_capability_list "ll,ii,dj,jw,ws:";
set $bypass_cache 0;
}
# All User Agents that represent
# 1) mobiles
# 2) tablets
# 3) desktop browsers that do not have defer-javascript capability at a minimum
# are made to go to the pagespeed server directly bypassing the proxy_cache.
if ($http_user_agent ~* "Firefox/[1-2]\.|MSIE [5-8]\.") {
set $ua_dependent_ps_capability_list "";
set $bypass_cache 1;
}
if ($http_user_agent ~* "Mozilla.*Android.*Mobile*|iPhone|BlackBerry|Opera Mobi|Opera Mini|SymbianOS|UP.Browser|J-PHONE|Profile/MIDP|portalmmm|DoCoMo|Obigo") {
# These are Mobile User Agents. We don't cache responses for these.
set $ua_dependent_ps_capability_list "";
set $bypass_cache 1;
}
if ($http_user_agent ~* "Android|iPad|TouchPad|Silk-Accelerated|Kindle Fire") {
# These are Tablet User Agents. We don't cache responses for these.
set $ua_dependent_ps_capability_list "";
set $bypass_cache 1;
}
# Skip the cache for .pagespeed. resource. PageSpeed has its own
# cache for these.
if ($uri ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+") {
set $bypass_cache 1;
}
if ($uri ~ "^/jscss/") {
set $bypass_cache 1;
}
if ($uri ~ "^/test/") {
set $bypass_cache 1;
}
# Block 3: Location block for purge requests.
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 209.20.65.89; # our ip: www.jefftk.com
deny all;
proxy_cache_purge htmlcache $gzip_support$ua_dependent_ps_capability_list$1$is_args$args;
}
# Block 4: Proxy everything except purge requests (above) in to PageSpeed.
location / {
proxy_pass http://www.jefftk.com:8090;
proxy_set_header Host $http_host;
proxy_cache htmlcache;
add_header X-Cache $upstream_cache_status;
proxy_cache_key $gzip_support$ua_dependent_ps_capability_list$uri$is_args$args;
proxy_cache_bypass $bypass_cache;
# Change control headers from the upstream, after using them to
# decide how long we should cache here.
proxy_hide_header Cache-Control;
add_header Cache-Control $new_cache_control_header_val;
# Everything is either html, in which case it's uncacheable by
# external caches, or longcached, in which case etag and
# last-modified are not needed.
proxy_hide_header ETag;
proxy_hide_header Last-Modified;
}
}
server {
listen 8090;
server_name www.jefftk.com;
root /var/www;
index index.html;
add_header Cache-Control "max-age=600";
pagespeed on;
pagespeed MessageBufferSize 200000;
#pagespeed LoadFromFile http://www.jefftk.com/ /var/www;
#pagespeed LoadFromFileRule Disallow /var/www/wsgi;
location /test {
pagespeed off;
}
location /jscss {
pagespeed off;
}
# Javascript outlining is off by default, but if I enable if for
# testing via a quaery param I want it to outline all the js.
pagespeed JsOutlineMinBytes 0;
#pagespeed InPlaceResourceOptimization on;
#pagespeed FileCacheCleanIntervalMs 10000000000;
#pagespeed FileCacheCleanIntervalMs 1000;
#pagespeed MetadataCacheStalenessThresholdMs 60000;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
pagespeed RewriteLevel CoreFilters;
#pagespeed MapProxyDomain http://www.jefftk.com:8050/ http://www.jefftk.com/;
pagespeed AnalyticsID "UA-27645543-1";
pagespeed EnableFilters add_instrumentation,remove_quotes;
pagespeed EnableFilters insert_ga,trim_urls;
pagespeed EnableFilters convert_jpeg_to_webp;
pagespeed RunExperiment off;
pagespeed ExperimentSpec "id=1;percent=50;default";
pagespeed ExperimentSpec "id=2;percent=50";
pagespeed Statistics on;
pagespeed StatisticsLogging on;
pagespeed LogDir /var/log/pagespeed;
pagespeed ModifyCachingHeaders off;
pagespeed DownstreamCachePurgeMethod "GET";
pagespeed DownstreamCachePurgeLocationPrefix "http://www.jefftk.com/purge";
location ~ \.html$ {
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
}
location ~ ^/?\+$ {
rewrite ^ https://plus.google.com/103013777355236494008/about permanent;
}
location / {
try_files $uri.html $uri $uri/ =404;
}
location /wsgi {
proxy_pass http://127.0.0.1:8080/wsgi/;
pagespeed off;
}
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
#location /ngx_pagespeed_statistics {
# allow 127.0.0.1;
# deny all;
#}
#location /ngx_pagespeed_message {
# allow 127.0.0.1;
# deny all;
#}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.trycontra.com;
root /var/www-tc;
index index.html;
charset utf8;
location /draft {
rewrite ^ / permanent;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment