Re: вопрос про кэш

Maxim Dounin mdounin на mdounin.ru
Чт Янв 12 18:18:17 UTC 2012


Hello!

On Thu, Jan 12, 2012 at 11:55:27AM +0400, Anton Kiryushkin wrote:

> И еще возник вопрос про
> proxy_cache_use_stale error timeout invalid_header updating http_500
> http_502 http_503 http_504 http_404;
> 
> Я правильно понимаю, что эта строчка означает примерно следующее:
> если код ответа бэкенда 500, 503, 502, 404, превысил таймаут, вернул
> некорректные заголовки и находится в процессе обновления - вернуть
> ответ из кэша, иначе возвращать ответ от бэкенда?

Да.

Наблюдаемое поведение - это ошибка, неправильно обрабатывался 
случай, когда все бекенды были признаны мёртвыми, и nginx просто 
не пытался никуда ходить (в error_log'е при этом возникает 
сообщение "no live upstreams", и все бекенды объявляются снова 
живыми).

Патч прилагается.

Maxim Dounin

> 
> 12 января 2012 г. 0:28 пользователь Anton Kiryushkin
> <swood at fotofor.biz> написал:
> > вдогонку, bcj = name_upstream.
> >
> > 12 января 2012 г. 0:27 пользователь Anton Kiryushkin
> > <swood at fotofor.biz> написал:
> >> Пишется следующее:
> >> """name_upstream""502""EXPIRED"
> >> или """server1, server2""502, 502""-"
> >> или """bcj""502""MISS"
> >>
> >> При этом время жизни кэша задано в 5 минут:
> >>        location = / {
> >>            proxy_ignore_headers Cache-Control Expires Set-Cookie;
> >>            proxy_cache_bypass  $http_x_update;
> >>
> >>            proxy_cache cache_page;
> >>            proxy_cache_key $scheme$host$request_uri;
> >>            proxy_cache_valid 5m;
> >>            proxy_hide_header "Set-Cookie";
> >>
> >>            proxy_cache_use_stale error invalid_header timeout
> >> updating http_500 http_502 http_503 http_504 http_404;
> >>
> >>            proxy_pass  http://backend;
> >>            proxy_set_header X-Real-IP $remote_addr;
> >>            proxy_set_header Host $http_host;
> >>            proxy_redirect default;
> >>            proxy_buffering on;
> >>
> >>        }
> >>
> >> 9 января 2012 г. 5:10 пользователь Maxim Dounin <mdounin at mdounin.ru> написал:
> >>> Hello!
> >>>
> >>> On Sun, Jan 08, 2012 at 09:24:58PM +0400, Anton Kiryushkin wrote:
> >>>
> >>>> Честно сказать дебаг я еще не делал. В обычном access стандартный ответ 502.
> >>>
> >>> Для начала - что в error_log'е, и что в переменных $upstream_addr,
> >>> $upstream_status, $upstream_cache_status (писать в access_log).
> >>>
> >>> Maxim Dounin
> >>>
> >>>>
> >>>> 8 января 2012 г. 16:52 пользователь Maxim Dounin <mdounin at mdounin.ru> написал:
> >>>> > Hello!
> >>>> >
> >>>> > On Fri, Jan 06, 2012 at 04:48:59PM +0400, Anton Kiryushkin wrote:
> >>>> >
> >>>> >> Как можно объяснить поведение nginx, при котором страница есть в кэше,
> >>>> >> бэкенд остановлен, а при ctrl + f5 ловится 502-я ошибка, хотя при
> >>>> >> возникновении этой ошибки ответ должен браться из кэша. Вот пример
> >>>> >> локейшена:
> >>>> >>
> >>>> >> location / {
> >>>> >>             proxy_ignore_headers Cache-Control Expires Set-Cookie;
> >>>> >>             proxy_hide_header "Set-Cookie";
> >>>> >>
> >>>> >>             proxy_cache cache_page;
> >>>> >>             proxy_cache_key $scheme$host;
> >>>> >>             proxy_cache_valid 1m;
> >>>> >>
> >>>> >>             proxy_cache_use_stale error timeout invalid_header
> >>>> >> updating http_500 http_502 http_503 http_504 http_404;
> >>>> >>
> >>>> >>             proxy_pass  http://backend;
> >>>> >>             proxy_set_header X-Real-IP $remote_addr;
> >>>> >>             proxy_set_header Host $http_host;
> >>>> >>             proxy_redirect default;
> >>>> >>             proxy_buffering on;
> >>>> >> }
> >>>> >>
> >>>> >> При этом если страница полностью прогрузилась, 502-я не ловится, а
> >>>> >> если догрузилась не доконца - то каждое второе срабатывание.
> >>>> >
> >>>> > А в логах при этом что?
> >>>> >
> >>>> > Maxim Dounin
> >>>> >
> >>>> > _______________________________________________
> >>>> > nginx-ru mailing list
> >>>> > nginx-ru at nginx.org
> >>>> > http://mailman.nginx.org/mailman/listinfo/nginx-ru
> >>>>
> >>>>
> >>>>
> >>>> --
> >>>> Best regards,
> >>>> Anton Kiryushkin,
> >>>> _______________________________________________
> >>>> nginx-ru mailing list
> >>>> nginx-ru at nginx.org
> >>>> http://mailman.nginx.org/mailman/listinfo/nginx-ru
> >>>
> >>> _______________________________________________
> >>> nginx-ru mailing list
> >>> nginx-ru at nginx.org
> >>> http://mailman.nginx.org/mailman/listinfo/nginx-ru
> >>
> >>
> >>
> >> --
> >> Best regards,
> >> Anton Kiryushkin,
> >
> >
> >
> > --
> > Best regards,
> > Anton Kiryushkin,
> 
> 
> 
> -- 
> Best regards,
> Anton Kiryushkin,
> _______________________________________________
> nginx-ru mailing list
> nginx-ru at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-ru
-------------- next part --------------
# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1326392142 -14400
# Node ID 346b6aef9d0df7cd34297fd9d79f1d67ef908d38
# Parent  7a274f0f9c211f770399aa23bf6f9c508a3e58c9
Fixed proxy_cache_use_stale in "no live upstreams" case.

diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c
--- a/src/http/modules/ngx_http_fastcgi_module.c
+++ b/src/http/modules/ngx_http_fastcgi_module.c
@@ -2314,6 +2314,10 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf
                                          |NGX_HTTP_UPSTREAM_FT_OFF;
     }
 
+    if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
+        conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
+    }
+
     if (conf->upstream.cache_methods == 0) {
         conf->upstream.cache_methods = prev->upstream.cache_methods;
     }
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -2667,17 +2667,21 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
                               (NGX_CONF_BITMASK_SET
                                |NGX_HTTP_UPSTREAM_FT_OFF));
 
-    if (conf->upstream.cache_methods == 0) {
-        conf->upstream.cache_methods = prev->upstream.cache_methods;
-    }
-
-    conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
-
     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
                                          |NGX_HTTP_UPSTREAM_FT_OFF;
     }
 
+    if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
+        conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
+    }
+
+    if (conf->upstream.cache_methods == 0) {
+        conf->upstream.cache_methods = prev->upstream.cache_methods;
+    }
+
+    conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
+
     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
                              prev->upstream.cache_bypass, NULL);
 
diff --git a/src/http/modules/ngx_http_scgi_module.c b/src/http/modules/ngx_http_scgi_module.c
--- a/src/http/modules/ngx_http_scgi_module.c
+++ b/src/http/modules/ngx_http_scgi_module.c
@@ -1286,6 +1286,10 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t 
                                          |NGX_HTTP_UPSTREAM_FT_OFF;
     }
 
+    if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
+        conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
+    }
+
     if (conf->upstream.cache_methods == 0) {
         conf->upstream.cache_methods = prev->upstream.cache_methods;
     }
diff --git a/src/http/modules/ngx_http_uwsgi_module.c b/src/http/modules/ngx_http_uwsgi_module.c
--- a/src/http/modules/ngx_http_uwsgi_module.c
+++ b/src/http/modules/ngx_http_uwsgi_module.c
@@ -1338,6 +1338,10 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t
                                          |NGX_HTTP_UPSTREAM_FT_OFF;
     }
 
+    if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
+        conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
+    }
+
     if (conf->upstream.cache_methods == 0) {
         conf->upstream.cache_methods = prev->upstream.cache_methods;
     }


Подробная информация о списке рассылки nginx-ru