[PATCH] Add 307 and 308 to default response codes for xxxxx_cache_valid.

Gena Makhomed gmm at csdoc.com
Fri Sep 21 15:35:52 UTC 2018


On 21.09.2018 15:58, Maxim Dounin wrote:

>> Allow 1xx 2xx 3xx 4xx 5xx codes in xxxxx_cache_valid directives.
>>
>> For example, config fragment
>>
>>       fastcgi_cache_valid 200 201 202 203 204 205 206 207 208 226 5m;
>>       fastcgi_cache_valid 300 301 302 303 304 305 306 307 308 10m;
>>
>> now can be rewritten as
>>
>>       fastcgi_cache_valid 2xx 5m;
>>       fastcgi_cache_valid 3xx 10m;

> I cannot say I like this change.  Cacheability of various response
> codes vary widely, and caching then "in bulk" in most cases is a
> bad idea.
> 
> In particular, caching of 201 is always wrong as it is only
> expected to be returned to non-cacheable POST and PUT requests.
> Caching 206 is wrong unless you use "Ranges" in the cache key.
> And caching 304 is wrong unless you've laso included
> If-Modified-Since and If-None-Match headers in the cache key.
> 
> As such, I would rather refrain from introducing these shortcuts,
> as they looks rather dangerous for the unwary.

Ok, thank you for detailed explanation.

What about adding 307 and 308 to list of default response codes?

# HG changeset patch
# User Gena Makhomed <gmm at csdoc.com>
# Date 1537543090 -10800
#      Fri Sep 21 18:18:10 2018 +0300
# Node ID c23c4b7822ae8b100c0add10d6a334f773f05c80
# Parent  87d2ea860f380dc8418c97c0163412f53c2d008e
Add 307 and 308 to default response codes for xxxxx_cache_valid.

If only caching time is specified

     fastcgi_cache_valid 5m;

then only 200, 301, 302, 307 and 308 responses are cached.

diff -r 87d2ea860f38 -r c23c4b7822ae src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Mon Sep 10 18:57:39 2018 +0300
+++ b/src/http/ngx_http_file_cache.c	Fri Sep 21 18:18:10 2018 +0300
@@ -2624,7 +2624,7 @@
      ngx_uint_t                i, n;
      ngx_array_t             **a;
      ngx_http_cache_valid_t   *v;
-    static ngx_uint_t         statuses[] = { 200, 301, 302 };
+    static ngx_uint_t         statuses[] = { 200, 301, 302, 307, 308 };

      a = (ngx_array_t **) (p + cmd->offset);

@@ -2647,7 +2647,7 @@

      if (n == 1) {

-        for (i = 0; i < 3; i++) {
+        for (i = 0; i < 5; i++) {
              v = ngx_array_push(*a);
              if (v == NULL) {
                  return NGX_CONF_ERROR;
@@ -2669,7 +2669,7 @@
          } else {

              status = ngx_atoi(value[i].data, value[i].len);
-            if (status < 100) {
+            if (status < 100 || status > 599) {
                  ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                     "invalid status \"%V\"", &value[i]);
                  return NGX_CONF_ERROR;


More information about the nginx-devel mailing list