41
Content Caching with NGINX and NGINX Plus Kevin Jones - Sales Engineer, NGINX Software Inc.

Content Caching with NGINX and NGINX Plus

Embed Size (px)

Citation preview

Page 1: Content Caching with NGINX and NGINX Plus

Content Caching with NGINX and NGINX Plus

Kevin Jones - Sales Engineer, NGINX Software Inc.

Page 2: Content Caching with NGINX and NGINX Plus

¯\_( )_/¯

Wiki + Links

Page 3: Content Caching with NGINX and NGINX Plus

Agenda

• Brief Overview of Basic Caching Directives • HA Caching Architectures • Byte Range Request Caching • Advanced Cache Control Using NGINX Plus

Page 4: Content Caching with NGINX and NGINX Plus

The Basics…The Basics…

Page 5: Content Caching with NGINX and NGINX Plus
Page 6: Content Caching with NGINX and NGINX Plus

proxy_cache_path

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;

Sets the path and other parameters of a cache.

Default: - Context: http

Page 7: Content Caching with NGINX and NGINX Plus

proxy_cache

location / { proxy_cache my_cache; proxy_pass http://my_upstream; }

Defines a shared memory zone used for caching.

Default: - Context: http, server, location

Page 8: Content Caching with NGINX and NGINX Plus

proxy_cache_key

proxy_cache_key $proxy_host$request_uri$cookie_foo;

Defines the key for caching.

Default: proxy_cache_key $scheme$proxy_host$request_uri; Context: http, server, location

Page 9: Content Caching with NGINX and NGINX Plus

proxy_cache_valid

proxy_cache_valid 200 301 302 1h; proxy_cache_valid any 1m;

Sets caching time for different response codes.

Default: - Context: http, server, location

Page 10: Content Caching with NGINX and NGINX Plus

proxy_cache_methods

proxy_cache_methods GET HEAD POST;

Configuration to specify HTTP methods that NGINX should cache.

Default: proxy_cache_methods GET HEAD; Context: http, server, location

Page 11: Content Caching with NGINX and NGINX Plus

proxy_cache_min_uses

proxy_cache_min_uses 3;

Sets the number of requests before a response is cached.

Default: proxy_cache_min_uses 1; Context: http, server, location

Page 12: Content Caching with NGINX and NGINX Plus

proxy_cache_lock

proxy_cache_lock on;

When enabled, only one request at a time will be allowed to populate a new cache element.

Default: proxy_cache_lock off; Context: http, server, location

Page 13: Content Caching with NGINX and NGINX Plus

proxy_cache_bypass

proxy_cache_bypass $arg_nocache $cookie_no_cache;

Defines the condition under which a response should not be taken from the cache.

Default: - Context: http, server, location

Page 14: Content Caching with NGINX and NGINX Plus

proxy_cache_use_stale

proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

Determines in which cases a stale cached response can be used when an error occurs to the proxied server.

Default: proxy_cache_use_stale off; Context: http, server, location

Page 15: Content Caching with NGINX and NGINX Plus

“Woah.. I know NGINX…”Learn MOAR here: https://www.nginx.com/blog/nginx-caching-guide/

Page 16: Content Caching with NGINX and NGINX Plus

High AvailabilityCaching

Page 17: Content Caching with NGINX and NGINX Plus

Why cluster?

Page 18: Content Caching with NGINX and NGINX Plus

Cost Impact

Page 19: Content Caching with NGINX and NGINX Plus

Why cant I just use shared volumes?

Page 20: Content Caching with NGINX and NGINX Plus

• Cache state is not synchronized • NGINX can be sensitive to disk latency, rw’s • Shared volume performance can be unreliable and

inconsistent

Page 21: Content Caching with NGINX and NGINX Plus

So what do we do?

Page 22: Content Caching with NGINX and NGINX Plus

Consistent Hash Architecture

• Hash based on proxy_cache_key • Shard(ed) cache • Effective use of storage • Reduces Impact but can be costly to implement

Page 23: Content Caching with NGINX and NGINX Plus

Is there a cheaper way?

Page 24: Content Caching with NGINX and NGINX Plus

Active / Passive HA Cluster

• Both instances have fully-populated caches • Minimal Cost and Impact

Page 25: Content Caching with NGINX and NGINX Plus

Active / Passive HA Cluster

On failure… “passive” node remains up with full cache

Page 26: Content Caching with NGINX and NGINX Plus

Active / Passive HA Cluster

“Active” node fetches from the origin when “passive” fails

Page 27: Content Caching with NGINX and NGINX Plus

Now something slightly more complicated but even more effective…

Page 28: Content Caching with NGINX and NGINX Plus

Active / Active HA Cluster

Most effective use of hardware, doubling usage capacity.

Page 29: Content Caching with NGINX and NGINX Plus

Can I spread my cache across multiple disks?

Page 30: Content Caching with NGINX and NGINX Plus

proxy_cache_path /path/to/hdd1 levels=1:2 keys_zone=my_cache_hdd1:10m max_size=10g inactive=60m use_temp_path=off;

proxy_cache_path /path/to/hdd2 levels=1:2 keys_zone=my_cache_hdd2:10m max_size=10g inactive=60m use_temp_path=off;

split_clients $request_uri $my_cache { 50% “my_cache_hdd1”; 50% “my_cache_hdd2”; }

server { ... location / { proxy_cache $my_cache; proxy_pass http://my_upstream; }}

Dynamic proxy_cache Example

Page 31: Content Caching with NGINX and NGINX Plus

Byte Range RequestCaching

Page 32: Content Caching with NGINX and NGINX Plus
Page 33: Content Caching with NGINX and NGINX Plus

proxy_cache_path /tmp/mycache keys_zone=my_cache:10m;

server { listen 80;

proxy_cache my_cache;

slice 1m; proxy_cache_key $host$uri$is_args$args$slice_range; proxy_set_header Range $slice_range; proxy_http_version 1.1; proxy_cache_valid 200 206 1h;

location / { proxy_pass http://origin:80; } }

Slice Example

Page 34: Content Caching with NGINX and NGINX Plus

Proxy Cache Control

Page 35: Content Caching with NGINX and NGINX Plus

Enter…

Page 36: Content Caching with NGINX and NGINX Plus

Go to www.nginx.com/developer-license Password: NGINX+MaxCDN (case sensitive)

Get started with an exclusive NGINX Plus Community Developer License:

Page 37: Content Caching with NGINX and NGINX Plus

proxy_cache_path /tmp/cache keys_zone=my_cache:10m levels=1:2 inactive=60s;

map $request_method $purge_method {PURGE 1;default 0;

}

server { listen 80; server_name www.example.com;

location / { proxy_pass http://localhost:8002; proxy_cache my_cache;

proxy_cache_purge $purge_method; } }

Cache Purge API Configuration

Page 38: Content Caching with NGINX and NGINX Plus

Can I restrict access to the cache purge API?

Page 39: Content Caching with NGINX and NGINX Plus

http {

… map $request_method $purge_method { PURGE $purge_allowed; default 0; } geo $purge_allowed { 127.0.0.0/24 1; # allow from localhost 192.168.1.1 1; 10.0.0.100 1; 172.0.200.10 1; default 0; # deny from other }

}

Restrict Access

Page 40: Content Caching with NGINX and NGINX Plus

Demo Time.

Page 41: Content Caching with NGINX and NGINX Plus

¯\_( )_/¯

Thanks for coming!