Caching is a powerful feature in NGINX that can significantly improve the performance and scalability of your web applications. By storing copies of frequently requested content, NGINX can serve these requests faster and reduce the load on your backend servers. In this section, we will cover the basics of caching, how to configure it in NGINX, and some advanced caching techniques.
Key Concepts
- Cache: A storage layer that saves copies of content to serve future requests faster.
- Cache Key: A unique identifier for each cached item, typically based on the request URL and other parameters.
- Cache Control: HTTP headers that dictate how and when content should be cached.
- Cache Purging: The process of removing outdated or invalid content from the cache.
Basic Caching Configuration
To enable caching in NGINX, you need to configure a few directives in your NGINX configuration file. Here is a basic example:
http { # Define a cache path proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off; server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; add_header X-Cache-Status $upstream_cache_status; } } }
Explanation
-
proxy_cache_path: Defines the location and parameters for the cache storage.
/var/cache/nginx
: Directory where cached files are stored.levels=1:2
: Directory structure for cache files.keys_zone=my_cache:10m
: Defines a shared memory zone namedmy_cache
with 10MB of storage for cache keys.max_size=1g
: Maximum size of the cache.inactive=60m
: Time after which inactive cache items are removed.use_temp_path=off
: Disables the use of a temporary path for cache files.
-
proxy_cache: Enables caching for the specified location and uses the
my_cache
zone. -
proxy_cache_valid: Sets the cache duration for different HTTP status codes.
200 302 10m
: Cache 200 (OK) and 302 (Found) responses for 10 minutes.404 1m
: Cache 404 (Not Found) responses for 1 minute.
-
add_header: Adds a custom header to the response to indicate the cache status.
Advanced Caching Techniques
Cache Purging
To remove specific items from the cache, you can use the ngx_cache_purge
module. This module allows you to purge cached content using a special URL.
location /purge { allow 127.0.0.1; deny all; proxy_cache_purge my_cache "$scheme://$host$request_uri"; }
Cache Bypassing
You can bypass the cache for certain requests using the proxy_cache_bypass
directive.
location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_bypass $cookie_nocache $arg_nocache; add_header X-Cache-Status $upstream_cache_status; }
Cache Locking
Cache locking prevents multiple requests for the same content from being sent to the backend server simultaneously.
Practical Exercise
Exercise 1: Basic Caching Setup
- Configure NGINX to cache responses from a backend server.
- Set the cache duration for 200 and 302 responses to 5 minutes.
- Add a custom header to indicate the cache status.
Solution:
http { proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off; server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_valid 200 302 5m; add_header X-Cache-Status $upstream_cache_status; } } }
Exercise 2: Cache Purging
- Enable cache purging for your NGINX server.
- Allow purging only from the localhost.
Solution:
http { proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off; server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_valid 200 302 5m; add_header X-Cache-Status $upstream_cache_status; } location /purge { allow 127.0.0.1; deny all; proxy_cache_purge my_cache "$scheme://$host$request_uri"; } } }
Common Mistakes and Tips
-
Mistake: Not setting the correct permissions for the cache directory.
- Tip: Ensure that the NGINX user has read and write permissions to the cache directory.
-
Mistake: Overlooking the
inactive
parameter, leading to a bloated cache.- Tip: Set appropriate
inactive
andmax_size
parameters to manage cache size effectively.
- Tip: Set appropriate
-
Mistake: Forgetting to add cache control headers.
- Tip: Use
add_header
to include cache status in responses for easier debugging.
- Tip: Use
Conclusion
In this section, we covered the basics of caching in NGINX, including how to configure caching, advanced techniques like cache purging and bypassing, and practical exercises to reinforce your understanding. Caching is a crucial feature for optimizing the performance of your web applications, and mastering it will help you build more efficient and scalable systems.
Next, we will delve into NGINX Modules in Module 5, where you will learn about extending NGINX functionality with various modules.