SlideShare a Scribd company logo
NGINX High-performance 
Caching 
Introduced by Andrew Alexeev 
Presented by Owen Garrett 
Nginx, Inc.
About this webinar 
Content Caching is one of the most effective ways to dramatically improve 
the performance of a web site. In this webinar, we’ll deep-dive into 
NGINX’s caching abilities and investigate the architecture used, debugging 
techniques and advanced configuration. By the end of the webinar, you’ll 
be well equipped to configure NGINX to cache content exactly as you need.
BASIC PRINCIPLES OF CONTENT CACHING
Basic Principles 
Internet 
N 
GET /index.html 
GET /index.html 
Used by: Browser Cache, Content Delivery Network and/or Reverse Proxy Cache
Mechanics of HTTP Caching 
• Origin server declares cacheability of content 
Expires: Tue, 6 May 2014 02:28:12 GMT 
Cache-Control: public, max-age=60 
X-Accel-Expires: 30 
Last-Modified: Tue, 29 April 2014 02:28:12 GMT 
ETag: "3e86-410-3596fbbc“ 
• Requesting client honors cacheability 
– May issue conditional GETs
What does NGINX cache? 
• Cache GET and HEAD with no Set-Cookie response 
• Uniqueness defined by raw URL or: 
proxy_cache_key $scheme$proxy_host$uri$is_args$args; 
• Cache time defined by 
– X-Accel-Expires 
– Cache-Control 
– Expires http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
NGINX IN OPERATION…
NGINX Config 
proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 inactive=60m; 
server { 
listen 80; 
server_name localhost; 
location / { 
proxy_pass http://localhost:8080; 
proxy_cache one; 
} 
}
Caching Process 
Internet 
MISS 
Read request Wait? 
Check Cache 
Respond from 
cache 
cache_lock_timeout 
Response 
cacheable? 
HIT 
Stream to disk 
NGINX can use stale content under the following circumstances: 
proxy_cache_use_stale error | timeout | invalid_header | 
updating | http_500 | http_502 | http_503 | http_504 | 
http_403 | http_404 | off
Caching is not just for HTTP 
• FastCGI 
– Functions much like HTTP 
• Memcache 
– Retrieve content from memcached 
server (must be prepopulated) 
• uwsgi and SCGI 
N 
HTTP 
FastCGI 
memcached 
uwsgi 
SCGI 
NGINX is more than 
just a reverse proxy
HOW TO UNDERSTAND WHAT’S GOING ON
Cache Instrumentation 
add_header X-Cache-Status $upstream_cache_status; 
MISS Response not found in cache; got from upstream. Response may have been 
saved to cache 
BYPASS proxy_cache_bypass got response from upstream. Response may have 
been saved to cache 
EXPIRED entry in cache has expired; we return fresh content from upstream 
STALE takes control and serves stale content from cache because upstream is not 
responding correctly 
UPDATING serve state content from cache because cache_lock has timed out and 
proxy_use_stale takes control 
REVALIDATED proxy_cache_revalidate verified that the current cached content was still 
valid (if-modified-since) 
HIT we serve valid, fresh content direct from cache
Cache Instrumentation 
map $remote_addr $cache_status { 
127.0.0.1 $upstream_cache_status; 
default “”; 
} 
server { 
location / { 
proxy_pass http://localhost:8002; 
proxy_cache one; 
add_header X-Cache-Status $cache_status; 
} 
}
Extended Status 
Check out: demo.nginx.com 
http://demo.nginx.com/status.html http://demo.nginx.com/status
HOW CONTENT CACHING FUNCTIONS 
IN NGINX
How it works... 
• NGINX uses a persistent disk-based cache 
– OS Page Cache keeps content in memory, with hints from 
NGINX processes 
• We’ll look at: 
– How is content stored in the cache? 
– How is the cache loaded at startup? 
– Pruning the cache over time 
– Purging content manually from the cache
How is cached content stored? 
proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 
max_size=40m; 
• Define cache key: 
proxy_cache_key $scheme$proxy_host$uri$is_args$args; 
• Get the content into the cache, then check the md5 
$ echo -n "httplocalhost:8002/time.php" | md5sum 
6d91b1ec887b7965d6a926cff19379b4 - 
• Verify it’s there: 
$ cat /tmp/cache/4/9b/6d91b1ec887b7965d6a926cff19379b4
Loading cache from disk 
• Cache metadata stored in shared memory segment 
• Populated at startup from cache by cache loader 
proxy_cache_path path keys_zone=name:size 
[loader_files=number] [loader_threshold=time] [loader_sleep=time]; 
(100) (200ms) (50ms) 
– Loads files in blocks of 100 
– Takes no longer than 200ms 
– Pauses for 50ms, then repeats
Managing the disk cache 
• Cache Manager runs periodically, purging files that 
were inactive irrespective of cache time, deleteing 
files in LRU style if cache is too big 
proxy_cache_path path keys_zone=name:size 
[inactive=time] [max_size=size]; 
(10m) 
– Remove files that have not been used within 10m 
– Remove files if cache size exceeds max_size
Purging content from disk 
• Find it and delete it 
– Relatively easy if you know the key 
• NGINX Plus – cache purge capability 
$ curl -X PURGE -D – "http://localhost:8001/*" 
HTTP/1.1 204 No Content 
Server: nginx/1.5.12 
Date: Sat, 03 May 2014 16:33:04 GMT 
Connection: keep-alive 
X-Cache-Key: httplocalhost:8002/*
CONTROLLING CACHING
Delayed caching 
proxy_cache_min_uses number; 
• Saves on disk writes for very cool caches 
Cache revalidation 
proxy_cache_revalidate on; 
• Saves on upstream bandwidth and disk writes
Control over cache time 
proxy_cache_valid 200 302 10m; 
proxy_cache_valid 404 1m; 
• Priority is: 
– X-Accel-Expires 
– Cache-Control 
– Expires 
– proxy_cache_valid 
Set-Cookie response header 
means no caching
Cache / don’t cache 
proxy_cache_bypass string ...; 
proxy_no_cache string ...; 
• Bypass the cache – go to origin; may cache result 
• No_Cache – if we go to origin, don’t cache result 
proxy_no_cache $cookie_nocache $arg_nocache $http_authorization; 
• Typically used with a complex cache key, and only if the 
origin does not sent appropriate cache-control reponses
Multiple Caches 
proxy_cache_path /tmp/cache1 keys_zone=one:10m levels=1:2 inactive=60s; 
proxy_cache_path /tmp/cache2 keys_zone=two:2m levels=1:2 inactive=20s; 
• Different cache policies for different tenants 
• Pin caches to specific disks 
• Temp-file considerations – put on same disk!: 
proxy_temp_path path [level1 [level2 [level3]]];
QUICK REVIEW – WHY CACHE?
Why is page speed important? 
• We used to talk about the ‘N second rule’: 
– 10-second rule 
• (Jakob Nielsen, March 1997) 
– 8-second rule 
• (Zona Research, June 2001) 
– 4-second rule 
• (Jupiter Research, June 2006) 
– 3-second rule 
• (PhocusWright, March 2010) 
12 
10 
8 
6 
4 
2 
0 
Jan-97 
Jan-98 
Jan-99 
Jan-00 
Jan-01 
Jan-02 
Jan-03 
Jan-04 
Jan-05 
Jan-06 
Jan-07 
Jan-08 
Jan-09 
Jan-10 
Jan-11 
Jan-12 
Jan-13 
Jan-14
Google changed the rules 
“We want you to be able to get 
from one page to another as 
quickly as you turn the page on 
a book” 
Urs Hölzle, Google
The costs of poor performance 
• Google: search enhancements cost 0.5s page load 
– Ad CTR dropped 20% 
• Amazon: Artificially increased page load by 100ms 
– Customer revenue dropped 1% 
• Walmart, Yahoo, Shopzilla, Edmunds, Mozilla… 
– All reported similar effects on revenue 
• Google Pagerank – Page Speed affects Page Rank 
– Time to First Byte is what appears to count
NGINX Caching lets you 
Improve end-user performance 
Consolidate and simplify your web infrastructure 
Increase server capacity 
Insulate yourself from server failures
Closing thoughts 
• 38% of the world’s busiest websites use NGINX 
• Check out the blogs on nginx.com 
• Future webinars: nginx.com/webinars 
Try NGINX F/OSS (nginx.org) or NGINX Plus (nginx.com)

More Related Content

What's hot (20)

PDF
VMware ESXi - Intel and Qlogic NIC throughput difference v0.6
David Pasek
 
PDF
Arm device tree and linux device drivers
Houcheng Lin
 
PDF
PostgreSQLの冗長化について
Soudai Sone
 
PPTX
VMware Performance Troubleshooting
glbsolutions
 
PDF
Velocity 2017 Performance analysis superpowers with Linux eBPF
Brendan Gregg
 
PPTX
Esxi troubleshooting
Ovi Chis
 
PDF
Deploying IPv6 on OpenStack
Vietnam Open Infrastructure User Group
 
PDF
Windows MSCS 운영 및 기타 설치 가이드
CheolHee Han
 
PPTX
Nginx
Geeta Vinnakota
 
PPTX
Linux Initialization Process (1)
shimosawa
 
PDF
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
PPTX
VMware Advance Troubleshooting Workshop - Day 3
Vepsun Technologies
 
PPTX
Apache Spark Architecture
Alexey Grishchenko
 
PPTX
Linux Network Stack
Adrien Mahieux
 
PDF
[오픈소스컨설팅]레드햇계열리눅스7 운영자가이드 - 기초편
Ji-Woong Choi
 
PPTX
Spark SQL Tutorial | Spark SQL Using Scala | Apache Spark Tutorial For Beginn...
Simplilearn
 
PPT
Basic Linux Internals
mukul bhardwaj
 
PDF
MariaDB MaxScale
MariaDB plc
 
PDF
GCP-pde.pdf
NirajKumar938204
 
PDF
Ceph c01
Lâm Đào
 
VMware ESXi - Intel and Qlogic NIC throughput difference v0.6
David Pasek
 
Arm device tree and linux device drivers
Houcheng Lin
 
PostgreSQLの冗長化について
Soudai Sone
 
VMware Performance Troubleshooting
glbsolutions
 
Velocity 2017 Performance analysis superpowers with Linux eBPF
Brendan Gregg
 
Esxi troubleshooting
Ovi Chis
 
Deploying IPv6 on OpenStack
Vietnam Open Infrastructure User Group
 
Windows MSCS 운영 및 기타 설치 가이드
CheolHee Han
 
Linux Initialization Process (1)
shimosawa
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
VMware Advance Troubleshooting Workshop - Day 3
Vepsun Technologies
 
Apache Spark Architecture
Alexey Grishchenko
 
Linux Network Stack
Adrien Mahieux
 
[오픈소스컨설팅]레드햇계열리눅스7 운영자가이드 - 기초편
Ji-Woong Choi
 
Spark SQL Tutorial | Spark SQL Using Scala | Apache Spark Tutorial For Beginn...
Simplilearn
 
Basic Linux Internals
mukul bhardwaj
 
MariaDB MaxScale
MariaDB plc
 
GCP-pde.pdf
NirajKumar938204
 
Ceph c01
Lâm Đào
 

Viewers also liked (12)

PDF
【Interop Tokyo 2015】 M 01: Cisco Meraki クラウド ネットワーキング
シスコシステムズ合同会社
 
PPTX
Velocity 2010 Highlights
Joshua Zhu
 
PDF
Tips on High Performance Server Programming
Joshua Zhu
 
PDF
阿里开源经验分享
Joshua Zhu
 
PDF
阿里CDN技术揭秘
Joshua Zhu
 
PDF
Nginx Internals
Joshua Zhu
 
PDF
阿里云CDN技术演进之路
Joshua Zhu
 
PDF
HTTP cache @ PUG Rome 03-29-2011
Alessandro Nadalin
 
PPTX
Load Balancing and Scaling with NGINX
NGINX, Inc.
 
PDF
Http cache - kiedy/dlaczego/jak
Paweł Mikołajczuk
 
PDF
20100918 android cache
rroijnoigntroie
 
PDF
So You Wanna Go Fast?
Tyler Treat
 
【Interop Tokyo 2015】 M 01: Cisco Meraki クラウド ネットワーキング
シスコシステムズ合同会社
 
Velocity 2010 Highlights
Joshua Zhu
 
Tips on High Performance Server Programming
Joshua Zhu
 
阿里开源经验分享
Joshua Zhu
 
阿里CDN技术揭秘
Joshua Zhu
 
Nginx Internals
Joshua Zhu
 
阿里云CDN技术演进之路
Joshua Zhu
 
HTTP cache @ PUG Rome 03-29-2011
Alessandro Nadalin
 
Load Balancing and Scaling with NGINX
NGINX, Inc.
 
Http cache - kiedy/dlaczego/jak
Paweł Mikołajczuk
 
20100918 android cache
rroijnoigntroie
 
So You Wanna Go Fast?
Tyler Treat
 
Ad

Similar to NGINX High-performance Caching (20)

PDF
High Availability Content Caching with NGINX
Kevin Jones
 
PDF
Using NGINX as an Effective and Highly Available Content Cache
Kevin Jones
 
PPTX
High Availability Content Caching with NGINX
NGINX, Inc.
 
PPTX
NGINX as a Content Cache
NGINX, Inc.
 
PDF
ITB2017 - Nginx Effective High Availability Content Caching
Ortus Solutions, Corp
 
PDF
Nginx caching
reneedv
 
PDF
What is Nginx and Why You Should to Use it with Wordpress Hosting
WPSFO Meetup Group
 
PPTX
NGINX for Application Delivery & Acceleration
NGINX, Inc.
 
PPTX
Delivering High Performance Websites with NGINX
NGINX, Inc.
 
PPTX
Nginx Scalable Stack
Bruno Paiuca
 
PPTX
Drupal 8 and NGINX
NGINX, Inc.
 
PPTX
5 things you didn't know nginx could do velocity
sarahnovotny
 
PPTX
Maximizing PHP Performance with NGINX
NGINX, Inc.
 
PPTX
NGINX: Basics and Best Practices
NGINX, Inc.
 
PPTX
NGINX 101: Web Server and Reverse-Proxy Cache
NGINX, Inc.
 
PPTX
5 things you didn't know nginx could do
sarahnovotny
 
KEY
Nginx - Tips and Tricks.
Harish S
 
PDF
tuning-nginx-for-high-performance-nick-shadrin.pdf
trihang02122018
 
PDF
NGINX: The Past, Present and Future of the Modern Web
Kevin Jones
 
PDF
ITB2017 - Nginx ppf intothebox_2017
Ortus Solutions, Corp
 
High Availability Content Caching with NGINX
Kevin Jones
 
Using NGINX as an Effective and Highly Available Content Cache
Kevin Jones
 
High Availability Content Caching with NGINX
NGINX, Inc.
 
NGINX as a Content Cache
NGINX, Inc.
 
ITB2017 - Nginx Effective High Availability Content Caching
Ortus Solutions, Corp
 
Nginx caching
reneedv
 
What is Nginx and Why You Should to Use it with Wordpress Hosting
WPSFO Meetup Group
 
NGINX for Application Delivery & Acceleration
NGINX, Inc.
 
Delivering High Performance Websites with NGINX
NGINX, Inc.
 
Nginx Scalable Stack
Bruno Paiuca
 
Drupal 8 and NGINX
NGINX, Inc.
 
5 things you didn't know nginx could do velocity
sarahnovotny
 
Maximizing PHP Performance with NGINX
NGINX, Inc.
 
NGINX: Basics and Best Practices
NGINX, Inc.
 
NGINX 101: Web Server and Reverse-Proxy Cache
NGINX, Inc.
 
5 things you didn't know nginx could do
sarahnovotny
 
Nginx - Tips and Tricks.
Harish S
 
tuning-nginx-for-high-performance-nick-shadrin.pdf
trihang02122018
 
NGINX: The Past, Present and Future of the Modern Web
Kevin Jones
 
ITB2017 - Nginx ppf intothebox_2017
Ortus Solutions, Corp
 
Ad

More from NGINX, Inc. (20)

PDF
【NGINXセミナー】 Ingressを使ってマイクロサービスの運用を楽にする方法
NGINX, Inc.
 
PDF
【NGINXセミナー】 NGINXのWAFとは?その使い方と設定方法 解説セミナー
NGINX, Inc.
 
PDF
【NGINXセミナー】API ゲートウェイとしてのNGINX Plus活用方法
NGINX, Inc.
 
PPTX
Get Hands-On with NGINX and QUIC+HTTP/3
NGINX, Inc.
 
PPTX
Managing Kubernetes Cost and Performance with NGINX & Kubecost
NGINX, Inc.
 
PDF
Manage Microservices Chaos and Complexity with Observability
NGINX, Inc.
 
PDF
Accelerate Microservices Deployments with Automation
NGINX, Inc.
 
PDF
Unit 2: Microservices Secrets Management 101
NGINX, Inc.
 
PDF
Unit 1: Apply the Twelve-Factor App to Microservices Architectures
NGINX, Inc.
 
PDF
NGINX基本セミナー(セキュリティ編)~NGINXでセキュアなプラットフォームを実現する方法!
NGINX, Inc.
 
PDF
Easily View, Manage, and Scale Your App Security with F5 NGINX
NGINX, Inc.
 
PDF
NGINXセミナー(基本編)~いまさら聞けないNGINXコンフィグなど基本がわかる!
NGINX, Inc.
 
PDF
Keep Ahead of Evolving Cyberattacks with OPSWAT and F5 NGINX
NGINX, Inc.
 
PPTX
Install and Configure NGINX Unit, the Universal Application, Web, and Proxy S...
NGINX, Inc.
 
PPTX
Protecting Apps from Hacks in Kubernetes with NGINX
NGINX, Inc.
 
PPTX
NGINX Kubernetes API
NGINX, Inc.
 
PPTX
Successfully Implement Your API Strategy with NGINX
NGINX, Inc.
 
PPTX
Installing and Configuring NGINX Open Source
NGINX, Inc.
 
PPTX
Shift Left for More Secure Apps with F5 NGINX
NGINX, Inc.
 
PPTX
How to Avoid the Top 5 NGINX Configuration Mistakes.pptx
NGINX, Inc.
 
【NGINXセミナー】 Ingressを使ってマイクロサービスの運用を楽にする方法
NGINX, Inc.
 
【NGINXセミナー】 NGINXのWAFとは?その使い方と設定方法 解説セミナー
NGINX, Inc.
 
【NGINXセミナー】API ゲートウェイとしてのNGINX Plus活用方法
NGINX, Inc.
 
Get Hands-On with NGINX and QUIC+HTTP/3
NGINX, Inc.
 
Managing Kubernetes Cost and Performance with NGINX & Kubecost
NGINX, Inc.
 
Manage Microservices Chaos and Complexity with Observability
NGINX, Inc.
 
Accelerate Microservices Deployments with Automation
NGINX, Inc.
 
Unit 2: Microservices Secrets Management 101
NGINX, Inc.
 
Unit 1: Apply the Twelve-Factor App to Microservices Architectures
NGINX, Inc.
 
NGINX基本セミナー(セキュリティ編)~NGINXでセキュアなプラットフォームを実現する方法!
NGINX, Inc.
 
Easily View, Manage, and Scale Your App Security with F5 NGINX
NGINX, Inc.
 
NGINXセミナー(基本編)~いまさら聞けないNGINXコンフィグなど基本がわかる!
NGINX, Inc.
 
Keep Ahead of Evolving Cyberattacks with OPSWAT and F5 NGINX
NGINX, Inc.
 
Install and Configure NGINX Unit, the Universal Application, Web, and Proxy S...
NGINX, Inc.
 
Protecting Apps from Hacks in Kubernetes with NGINX
NGINX, Inc.
 
NGINX Kubernetes API
NGINX, Inc.
 
Successfully Implement Your API Strategy with NGINX
NGINX, Inc.
 
Installing and Configuring NGINX Open Source
NGINX, Inc.
 
Shift Left for More Secure Apps with F5 NGINX
NGINX, Inc.
 
How to Avoid the Top 5 NGINX Configuration Mistakes.pptx
NGINX, Inc.
 

Recently uploaded (20)

PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 

NGINX High-performance Caching

  • 1. NGINX High-performance Caching Introduced by Andrew Alexeev Presented by Owen Garrett Nginx, Inc.
  • 2. About this webinar Content Caching is one of the most effective ways to dramatically improve the performance of a web site. In this webinar, we’ll deep-dive into NGINX’s caching abilities and investigate the architecture used, debugging techniques and advanced configuration. By the end of the webinar, you’ll be well equipped to configure NGINX to cache content exactly as you need.
  • 3. BASIC PRINCIPLES OF CONTENT CACHING
  • 4. Basic Principles Internet N GET /index.html GET /index.html Used by: Browser Cache, Content Delivery Network and/or Reverse Proxy Cache
  • 5. Mechanics of HTTP Caching • Origin server declares cacheability of content Expires: Tue, 6 May 2014 02:28:12 GMT Cache-Control: public, max-age=60 X-Accel-Expires: 30 Last-Modified: Tue, 29 April 2014 02:28:12 GMT ETag: "3e86-410-3596fbbc“ • Requesting client honors cacheability – May issue conditional GETs
  • 6. What does NGINX cache? • Cache GET and HEAD with no Set-Cookie response • Uniqueness defined by raw URL or: proxy_cache_key $scheme$proxy_host$uri$is_args$args; • Cache time defined by – X-Accel-Expires – Cache-Control – Expires http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
  • 8. NGINX Config proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 inactive=60m; server { listen 80; server_name localhost; location / { proxy_pass http://localhost:8080; proxy_cache one; } }
  • 9. Caching Process Internet MISS Read request Wait? Check Cache Respond from cache cache_lock_timeout Response cacheable? HIT Stream to disk NGINX can use stale content under the following circumstances: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off
  • 10. Caching is not just for HTTP • FastCGI – Functions much like HTTP • Memcache – Retrieve content from memcached server (must be prepopulated) • uwsgi and SCGI N HTTP FastCGI memcached uwsgi SCGI NGINX is more than just a reverse proxy
  • 11. HOW TO UNDERSTAND WHAT’S GOING ON
  • 12. Cache Instrumentation add_header X-Cache-Status $upstream_cache_status; MISS Response not found in cache; got from upstream. Response may have been saved to cache BYPASS proxy_cache_bypass got response from upstream. Response may have been saved to cache EXPIRED entry in cache has expired; we return fresh content from upstream STALE takes control and serves stale content from cache because upstream is not responding correctly UPDATING serve state content from cache because cache_lock has timed out and proxy_use_stale takes control REVALIDATED proxy_cache_revalidate verified that the current cached content was still valid (if-modified-since) HIT we serve valid, fresh content direct from cache
  • 13. Cache Instrumentation map $remote_addr $cache_status { 127.0.0.1 $upstream_cache_status; default “”; } server { location / { proxy_pass http://localhost:8002; proxy_cache one; add_header X-Cache-Status $cache_status; } }
  • 14. Extended Status Check out: demo.nginx.com http://demo.nginx.com/status.html http://demo.nginx.com/status
  • 15. HOW CONTENT CACHING FUNCTIONS IN NGINX
  • 16. How it works... • NGINX uses a persistent disk-based cache – OS Page Cache keeps content in memory, with hints from NGINX processes • We’ll look at: – How is content stored in the cache? – How is the cache loaded at startup? – Pruning the cache over time – Purging content manually from the cache
  • 17. How is cached content stored? proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 max_size=40m; • Define cache key: proxy_cache_key $scheme$proxy_host$uri$is_args$args; • Get the content into the cache, then check the md5 $ echo -n "httplocalhost:8002/time.php" | md5sum 6d91b1ec887b7965d6a926cff19379b4 - • Verify it’s there: $ cat /tmp/cache/4/9b/6d91b1ec887b7965d6a926cff19379b4
  • 18. Loading cache from disk • Cache metadata stored in shared memory segment • Populated at startup from cache by cache loader proxy_cache_path path keys_zone=name:size [loader_files=number] [loader_threshold=time] [loader_sleep=time]; (100) (200ms) (50ms) – Loads files in blocks of 100 – Takes no longer than 200ms – Pauses for 50ms, then repeats
  • 19. Managing the disk cache • Cache Manager runs periodically, purging files that were inactive irrespective of cache time, deleteing files in LRU style if cache is too big proxy_cache_path path keys_zone=name:size [inactive=time] [max_size=size]; (10m) – Remove files that have not been used within 10m – Remove files if cache size exceeds max_size
  • 20. Purging content from disk • Find it and delete it – Relatively easy if you know the key • NGINX Plus – cache purge capability $ curl -X PURGE -D – "http://localhost:8001/*" HTTP/1.1 204 No Content Server: nginx/1.5.12 Date: Sat, 03 May 2014 16:33:04 GMT Connection: keep-alive X-Cache-Key: httplocalhost:8002/*
  • 22. Delayed caching proxy_cache_min_uses number; • Saves on disk writes for very cool caches Cache revalidation proxy_cache_revalidate on; • Saves on upstream bandwidth and disk writes
  • 23. Control over cache time proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; • Priority is: – X-Accel-Expires – Cache-Control – Expires – proxy_cache_valid Set-Cookie response header means no caching
  • 24. Cache / don’t cache proxy_cache_bypass string ...; proxy_no_cache string ...; • Bypass the cache – go to origin; may cache result • No_Cache – if we go to origin, don’t cache result proxy_no_cache $cookie_nocache $arg_nocache $http_authorization; • Typically used with a complex cache key, and only if the origin does not sent appropriate cache-control reponses
  • 25. Multiple Caches proxy_cache_path /tmp/cache1 keys_zone=one:10m levels=1:2 inactive=60s; proxy_cache_path /tmp/cache2 keys_zone=two:2m levels=1:2 inactive=20s; • Different cache policies for different tenants • Pin caches to specific disks • Temp-file considerations – put on same disk!: proxy_temp_path path [level1 [level2 [level3]]];
  • 26. QUICK REVIEW – WHY CACHE?
  • 27. Why is page speed important? • We used to talk about the ‘N second rule’: – 10-second rule • (Jakob Nielsen, March 1997) – 8-second rule • (Zona Research, June 2001) – 4-second rule • (Jupiter Research, June 2006) – 3-second rule • (PhocusWright, March 2010) 12 10 8 6 4 2 0 Jan-97 Jan-98 Jan-99 Jan-00 Jan-01 Jan-02 Jan-03 Jan-04 Jan-05 Jan-06 Jan-07 Jan-08 Jan-09 Jan-10 Jan-11 Jan-12 Jan-13 Jan-14
  • 28. Google changed the rules “We want you to be able to get from one page to another as quickly as you turn the page on a book” Urs Hölzle, Google
  • 29. The costs of poor performance • Google: search enhancements cost 0.5s page load – Ad CTR dropped 20% • Amazon: Artificially increased page load by 100ms – Customer revenue dropped 1% • Walmart, Yahoo, Shopzilla, Edmunds, Mozilla… – All reported similar effects on revenue • Google Pagerank – Page Speed affects Page Rank – Time to First Byte is what appears to count
  • 30. NGINX Caching lets you Improve end-user performance Consolidate and simplify your web infrastructure Increase server capacity Insulate yourself from server failures
  • 31. Closing thoughts • 38% of the world’s busiest websites use NGINX • Check out the blogs on nginx.com • Future webinars: nginx.com/webinars Try NGINX F/OSS (nginx.org) or NGINX Plus (nginx.com)

Editor's Notes

  • #5: Why cache – three reasons – performance improvements, capacity improvements, and resilience to failures in backends
  • #8: Cool because is trivial to configure
  • #10: Error: an error occurred while establishing a connection with the server, passing a request to it, or reading the response header; Timeout: a timeout has occurred while establishing a connection with the server, passing a request to it, or reading the response header; invalid_header: a server returned an empty or invalid response; Updating – content is being refreshed and a lock is in place http_500: a server returned a response with the code 500; http_502: a server returned a response with the code 502; http_503: a server returned a response with the code 503; http_504: a server returned a response with the code 504; http_403: a server returned a response with the code 403; http_404: a server returned a response with the code 404; Off: disables passing a request to the next server.
  • #12: Complex. We make it really easy
  • #16: It uses same tech as static content that nginx is renowned for
  • #22: Get smart
  • #31: http://www.strangeloopnetworks.com/assets/images/infographic2.jpg http://www.thinkwithgoogle.com/articles/the-google-gospel-of-speed-urs-hoelzle.html http://moz.com/blog/how-website-speed-actually-impacts-search-ranking What does performance really mean to you? Revenue Ad CTR Employee and partner satisfaction What devices do your users use? What network conditions are they under?
  • #32: 1. Deliver all content at the speed of nginx 2. Compared to multiple point solutions 3. Cache for one second example 4. proxy_cache_use_stale