Mastering Nginx: A Beginner's Guide to High-Performance Web Servers

Nginx FastCGI and Proxy Caching

Section 4

Caching for Performance

Mastering Nginx: A Beginner's Guide to High-Performance Web ServersCaching for Performance

In the quest for a lightning-fast web server, caching plays a pivotal role. While Nginx excels at serving static assets quickly, its ability to cache dynamic content generated by backend applications is equally crucial for high-performance websites. This section delves into two powerful caching mechanisms Nginx offers for dynamic content: Nginx FastCGI caching and Nginx proxy caching.

Many web applications are built using languages like PHP, Python, or Ruby, which often communicate with Nginx via the FastCGI protocol. Nginx FastCGI caching allows Nginx to store the responses from your FastCGI backend (like PHP-FPM) and serve them directly to subsequent requests without needing to hit the backend application for every single visitor. This significantly reduces the load on your application servers and dramatically speeds up response times for frequently accessed dynamic content.

To enable FastCGI caching, you'll typically need to configure a cache zone and then instruct Nginx to use it for your FastCGI locations. Here's a breakdown of the key directives:

http {
    # Define a FastCGI cache zone
    fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=my_fastcgi_cache:10m inactive=60m;

    server {
        location / {
            # ... your existing FastCGI setup ...
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            include fastcgi_params;

            # Enable FastCGI caching
            fastcgi_cache my_fastcgi_cache;
            fastcgi_cache_valid 200 302 10m; # Cache successful responses for 10 minutes
            fastcgi_cache_valid 404 1m;    # Cache not found responses for 1 minute
            fastcgi_cache_key "$scheme$request_method$host$request_uri";
            add_header X-Cache-Status $fastcgi_cache_status;
        }
    }
}

Let's break down the important directives in the fastcgi_cache_path directive:

  • fastcgi_cache_path /var/cache/nginx/fastcgi: Specifies the directory where Nginx will store the cached files.
  • levels=1:2: Defines the directory structure for caching. This creates a two-level directory hierarchy to prevent issues with too many files in a single directory.
  • keys_zone=my_fastcgi_cache:10m: Creates a shared memory zone named my_fastcgi_cache with a size of 10 megabytes to store cache keys and metadata.
  • inactive=60m: Specifies how long cached items that haven't been accessed will be kept before being removed, even if their fastcgi_cache_valid time hasn't expired.

Inside the location block, these directives are key:

  • fastcgi_cache my_fastcgi_cache;: Enables caching for this location using the previously defined keys_zone.
  • fastcgi_cache_valid 200 302 10m;: Tells Nginx to cache responses with status codes 200 (OK) and 302 (Found) for 10 minutes. You can specify different durations for different status codes.
  • fastcgi_cache_valid 404 1m;: Caches 404 (Not Found) responses for 1 minute. This is useful to prevent repeated requests for non-existent pages.
  • fastcgi_cache_key "$scheme$request_method$host$request_uri";: Defines the unique key for each cached item. This ensures that different requests (e.g., GET vs. POST, or with different query parameters) are cached separately.
  • add_header X-Cache-Status $fastcgi_cache_status;: This is a very useful directive for debugging. It adds a header to the response indicating whether the content was served from the cache (HIT), bypasses the cache (BYPASS), or if the cache was updated (UPDATING).
チャプターへ戻る