40
You Can Run Faster! Caching with .

Caching with Nginx. You Can Run Faster!

Embed Size (px)

Citation preview

You Can Run Faster!Caching with .

Introduction

The Caching Concept

Initial Situation

The Caching Concept

The Caching Concept

The Caching Concept

The Caching Concept

Caching with Nginx

Caching with Nginx

Caching with Nginx

Nginx Cache Configuration

Configuration - Available Directives

proxy_cache_path /data/nginx/cache keys_zone=zone_name_one:10m;

proxy_cache zone_name_one;

Configuration - Available Directives

proxy_cache_path /data/nginx/cache keys_zone=one:10m max_size=200m;

proxy_cache_path /data/nginx/cache keys_zone=one:10m inactive=60m;

Configuration - Available Directives

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

Quiz

Quiz - Solution

proxy_cache_path /data/nginx/cache levels=2:4 keys_zone=one:10m;

What/When to cache?

proxy_cache_methods GET HEAD POST;

proxy_cache_min_uses 5;

proxy_cache_revalidate on;

How to cache?

proxy_cache_key "$host$request_uri$cookie_user";

proxy_cache_key "$scheme$proxy_host$uri$is_args$args";

How to generate a Nginx cache hash key?

echo -n "httpczerasz.com/time.php" | md5sum

When not to cache?

proxy_no_cache $http_pragma $http_authorization $cookie_nocache $arg_nocache;

How long to cache?

proxy_cache_valid any 1m;proxy_cache_valid 200 302 10m;

How long to cache?

Other Configuration Options

proxy_cache_use_stale error timeout;

proxy_cache_lock on;

proxy_temp_path /tmp/custom_cache/;

Debugging Nginx's Cache

Debugging Nginx's Cache

map $remote_addr $cache_status { 127.0.0.1 $upstream_cache_status; default "";}...add_header X-Cache-Status $cache_status;

MISS Object was not found in the cache. Response was served from the origin.

Response may have been saved to cache.

BYPASS Got response from upstream. Response may have been saved to cache.

EXPIRED Cached object has expired. Response was served from the upstream.

STALE Object served from cache because of issues with origin server response

UPDATING Serve stale content from cache because proxy_cache_lock has timed out

and proxy_use_stale takes control

REVALIDATED proxy_cache_revalidate verified that the current cached content was still

valid

HIT The object was found in the cache and it is served from there

Cache Loader

proxy_cache_path /data/nginx/cache keys_zone=one:10m [loader_files=number] [loader_sleep=time] [loader_threshold=time];

Cache Manager

proxy_cache_path /data/nginx/cache keys_zone=one:10m max_size=200m;

Example

How to create a small CDN with Nginx?

Muchas Gracias