Difference between revisions of "Nginx/Caching"

From WhyAskWhy.org Wiki
Jump to: navigation, search
(Stub page)
 
m (Added examples of what does/doesn't work.)
Line 22: Line 22:
 
Parameters of caching can also be set directly in the response header. This has a higher precedence than setting of caching time using the directive. The <code>X-Accel-Expires</code> header field sets caching time of a response in seconds. '''The value 0 disables the caching of a response'''. If a value starts with the prefix <code>@</code>, it sets an absolute time in seconds since Epoch, up to which the response may be cached. If header does not include the <code>X-Accel-Expires</code> field, parameters of caching may be set in the header fields <code>Expires</code> or <code>Cache-Control</code>. If a header includes the <code>Set-Cookie</code> field, such a response will not be cached. Processing of one or more of these response header fields can be disabled using the <code>proxy_ignore_headers</code> directive.
 
Parameters of caching can also be set directly in the response header. This has a higher precedence than setting of caching time using the directive. The <code>X-Accel-Expires</code> header field sets caching time of a response in seconds. '''The value 0 disables the caching of a response'''. If a value starts with the prefix <code>@</code>, it sets an absolute time in seconds since Epoch, up to which the response may be cached. If header does not include the <code>X-Accel-Expires</code> field, parameters of caching may be set in the header fields <code>Expires</code> or <code>Cache-Control</code>. If a header includes the <code>Set-Cookie</code> field, such a response will not be cached. Processing of one or more of these response header fields can be disabled using the <code>proxy_ignore_headers</code> directive.
 
</blockquote>
 
</blockquote>
 +
 +
 +
=== What works ===
 +
 +
So far I've determined that a nested setup works:
 +
 +
<syntaxhighlight lang="nginx">
 +
 +
    # Nested location block (PHP/FastCGI content)
 +
 +
    location /
 +
    {
 +
        proxy_buffering on;
 +
        proxy_cache proxy_cache;
 +
        proxy_cache_valid 200 302 3d;
 +
        proxy_cache_valid 404 1m;
 +
        proxy_cache_use_stale  error timeout invalid_header http_500;
 +
        proxy_cache_key    "$scheme$request_method$host$uri$is_args$args";
 +
 +
 +
        fastcgi_cache fastcgi_cache;
 +
        fastcgi_cache_valid 200 302 3d;
 +
        fastcgi_cache_valid 404 1m;
 +
        fastcgi_cache_use_stale  error timeout invalid_header http_500;
 +
        fastcgi_cache_key  "$scheme$request_method$host$uri$is_args$args";
 +
       
 +
        # Will content be cached if embedded within another location block?
 +
        # In this configuration, the answer is yes.
 +
        location ~ \.php$
 +
        {
 +
 +
          # Placing these in a separate file for now so they can be referenced
 +
          # directly when needed (until I learn of a better way)
 +
          include php_parameters.conf;
 +
 +
          # Misc general FastCGI and Proxy settings.
 +
          include proxy.inc.conf;
 +
        }
 +
       
 +
    }
 +
</syntaxhighlight>
 +
 +
=== What does not work ===
 +
 +
Not all that surprising, but this does not result in cached FastCGI (PHP) content:
 +
 +
<syntaxhighlight lang="nginx">
 +
    # Location blocks on the same level
 +
 +
    location /
 +
    {
 +
        proxy_buffering on;
 +
        proxy_cache proxy_cache;
 +
        proxy_cache_valid 200 302 3d;
 +
        proxy_cache_valid 404 1m;
 +
        proxy_cache_use_stale  error timeout invalid_header http_500;
 +
        proxy_cache_key    "$scheme$request_method$host$uri$is_args$args";
 +
 +
 +
        fastcgi_cache fastcgi_cache;
 +
        fastcgi_cache_valid 200 302 3d;
 +
        fastcgi_cache_valid 404 1m;
 +
        fastcgi_cache_use_stale  error timeout invalid_header http_500;
 +
        fastcgi_cache_key  "$scheme$request_method$host$uri$is_args$args";
 +
    }
 +
 +
    # Content matching this location block will not be cached
 +
    location ~ \.php$
 +
    {
 +
 +
      # Placing these in a separate file for now so they can be referenced
 +
      # directly when needed (until I learn of a better way)
 +
      include php_parameters.conf;
 +
 +
      # Misc general FastCGI and Proxy settings.
 +
      include proxy.inc.conf;
 +
    }
 +
</syntaxhighlight>
  
 
== References ==
 
== References ==

Revision as of 05:45, 19 August 2012



Pay close attention to what version of nginx a book or web resource is based on as behavior may change between releases [1]


For the last few weeks I've been trying to figure out how to properly configure caching for nginx sites and have had hit/miss (pun intended) results. The notes here are my attempt to keep up with what has worked so I can go from there.

Caching FastCGI content

From the FastCGI module wiki page:

The cache honors backend's Cache-Control, Expires, and etc. since version 0.7.48, Cache-Control: private and no-store only since 0.7.66, though. Vary handling is not implemented.

From the http_proxy_module page:

Parameters of caching can also be set directly in the response header. This has a higher precedence than setting of caching time using the directive. The X-Accel-Expires header field sets caching time of a response in seconds. The value 0 disables the caching of a response. If a value starts with the prefix @, it sets an absolute time in seconds since Epoch, up to which the response may be cached. If header does not include the X-Accel-Expires field, parameters of caching may be set in the header fields Expires or Cache-Control. If a header includes the Set-Cookie field, such a response will not be cached. Processing of one or more of these response header fields can be disabled using the proxy_ignore_headers directive.


What works

So far I've determined that a nested setup works:

    # Nested location block (PHP/FastCGI content)

    location /
    {
        proxy_buffering on;
        proxy_cache proxy_cache;
        proxy_cache_valid 200 302 3d;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale   error timeout invalid_header http_500;
        proxy_cache_key    "$scheme$request_method$host$uri$is_args$args";


        fastcgi_cache fastcgi_cache;
        fastcgi_cache_valid 200 302 3d;
        fastcgi_cache_valid 404 1m;
        fastcgi_cache_use_stale   error timeout invalid_header http_500;
        fastcgi_cache_key   "$scheme$request_method$host$uri$is_args$args";
        
        # Will content be cached if embedded within another location block?
        # In this configuration, the answer is yes.
        location ~ \.php$ 
        {

           # Placing these in a separate file for now so they can be referenced
           # directly when needed (until I learn of a better way)
           include php_parameters.conf;

           # Misc general FastCGI and Proxy settings.
           include proxy.inc.conf;
        }
        
    }

What does not work

Not all that surprising, but this does not result in cached FastCGI (PHP) content:

    # Location blocks on the same level

    location /
    {
        proxy_buffering on;
        proxy_cache proxy_cache;
        proxy_cache_valid 200 302 3d;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale   error timeout invalid_header http_500;
        proxy_cache_key    "$scheme$request_method$host$uri$is_args$args";


        fastcgi_cache fastcgi_cache;
        fastcgi_cache_valid 200 302 3d;
        fastcgi_cache_valid 404 1m;
        fastcgi_cache_use_stale   error timeout invalid_header http_500;
        fastcgi_cache_key   "$scheme$request_method$host$uri$is_args$args";
    }

    # Content matching this location block will not be cached
    location ~ \.php$ 
    {

       # Placing these in a separate file for now so they can be referenced
       # directly when needed (until I learn of a better way)
       include php_parameters.conf;

       # Misc general FastCGI and Proxy settings.
       include proxy.inc.conf;
    }

References