Content Caching with NGINX and NGINX Plus

Post on 09-Jan-2017

329 views 3 download

transcript

Content Caching with NGINX and NGINX Plus

Kevin Jones - Sales Engineer, NGINX Software Inc.

¯\_( )_/¯

Wiki + Links

Agenda

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

The Basics…The Basics…

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

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

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

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

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

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

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

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

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

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

High AvailabilityCaching

Why cluster?

Cost Impact

Why cant I just use shared volumes?

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

inconsistent

So what do we do?

Consistent Hash Architecture

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

Is there a cheaper way?

Active / Passive HA Cluster

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

Active / Passive HA Cluster

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

Active / Passive HA Cluster

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

Now something slightly more complicated but even more effective…

Active / Active HA Cluster

Most effective use of hardware, doubling usage capacity.

Can I spread my cache across multiple disks?

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

Byte Range RequestCaching

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

Proxy Cache Control

Enter…

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

Get started with an exclusive NGINX Plus Community Developer License:

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

Can I restrict access to the cache purge API?

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

Demo Time.

¯\_( )_/¯

Thanks for coming!