Top Banner
CACHING WITH NGINX By: Renée Hendricksen @gigglegirl4e
18

Nginx caching

Jan 09, 2017

Download

Technology

reneedv
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Nginx caching

CACHING WITH NGINXBy: Renée Hendricksen

@gigglegirl4e

Page 2: Nginx caching

WHAT IS CACHING?Where is your stuff coming from?

Page 3: Nginx caching

WHEN SHOULD I USE

CACHING?Speed (Size / Distance / Connectivity)

Capacity

Availability

Page 4: Nginx caching

TYPES OF CACHING

• Browser or Client Side Cache

• Proxy or Remote Cache

• Gateway Cache or Reverse Proxy Cache or Surrogate Cache

Page 5: Nginx caching

BROWSER OR CLIENT SIDE CACHE

• built into browser

• uses user’s local memory

• can be used for ‘offline mode’

• user can configure this

Page 6: Nginx caching

PROXY OR REMOTE CACHE

• On network infrastructure like firewall or VPN or packet shapers

• Configured by network administrators

• Can be used to control content as well as serve it

Page 7: Nginx caching

GATEWAY CACHE

• CDNs

• Load Balancers

• Web Server (vs application server)

Page 8: Nginx caching

WHAT IS CACHINGStoring Stuff Somewhere for “easy” retrieval later

Browser Firewall

Web Server

Load Balancer

CDN

Application Server

Database

LB Cache

Server Cache

App Cache

Browser Cache

Page 9: Nginx caching

STATIC VS DYNAMIC CONTENT

• Assets (images, css, js)

• Static Pages (rendered html)

• VS

• Pages that change based on user input, relayed information, or updated data (forms, chats/games, database/CMS)

Page 10: Nginx caching

HOW TO CACHE WITH NGINX

• proxy_cache_path - directive to configure your cache (where, how, name, etc…)

• proxy_cache - location directive to specify your cache by name (the one you configured above)

Page 11: Nginx caching

HOW TO CACHEwith NGINX

Page 12: Nginx caching

HTTP CACHE Headers (a selection)

• cache-control

• cache-control: private, max-age=0, no-cache

• expires (max-age overrides this)

• etag - entity-tag

• every update to a cached resource generates a new tag

• how to check if you need to get a new version of the resource

• private | public (user specific cache like a cookie vs shared cached resource like and image)

• no-cache (uses etag for revalidation)

• no-store (don’t cache this)

Page 13: Nginx caching

SIMPLE EXAMPLEpics are cached on the nginx cache server

*not actually using proxy_pass - it’s all on the same server

Page 14: Nginx caching

MORE COMPLEX EXAMPLE

Page 15: Nginx caching

HOW TO TEST• I used the rails request logs vs the nginx request logs to test

my simple example (also look at the cache folder after the first request)

• As a user you can use inspect and the network tab - where are the assets coming from - and inspect the headers - you can see HIT or MISS

• Cache Monitoring (NGINX Plus does this for you) - New Relic is great for this

Page 16: Nginx caching

A WORD ON SSL• Cache the connection

parameters for the session (Speedy SSL)

• ssl_session_cache

• ssl_session_timeout

• SSL Offloading w/ Nginx caches the static content as normal at the Nginx reverse proxy server

Page 17: Nginx caching

RESOURCES• NGINX Guides

• https://www.nginx.com/blog/nginx-caching-guide/

• NGINX Content Caching • https://www.nginx.com/resources/admin-guide/

content-caching/• Beginners Guide to CACHE Headers

• http://dev.mobify.com/blog/beginners-guide-to-http-cache-headers/

• Optimizing HTTPS with NGINX • https://bjornjohansen.no/optimizing-https-nginx

Page 18: Nginx caching

QUESTIONS?Thanks!