comparison src/http/ngx_http_upstream.c @ 5441:43ccaf8e8728

Upstream: cache revalidation with conditional requests. The following new directives are introduced: proxy_cache_revalidate, fastcgi_cache_revalidate, scgi_cache_revalidate, uwsgi_cache_revalidate. Default is off. When set to on, they enable cache revalidation using conditional requests with If-Modified-Since for expired cache items. As of now, no attempts are made to merge headers given in a 304 response during cache revalidation with headers previously stored in a cache item. Headers in a 304 response are only used to calculate new validity time of a cache item.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 18 Nov 2013 20:48:22 +0400
parents 16b68c724438
children b7b8e2fa7ebd
comparison
equal deleted inserted replaced
5440:cbb9a6c7493c 5441:43ccaf8e8728
14 static ngx_int_t ngx_http_upstream_cache(ngx_http_request_t *r, 14 static ngx_int_t ngx_http_upstream_cache(ngx_http_request_t *r,
15 ngx_http_upstream_t *u); 15 ngx_http_upstream_t *u);
16 static ngx_int_t ngx_http_upstream_cache_send(ngx_http_request_t *r, 16 static ngx_int_t ngx_http_upstream_cache_send(ngx_http_request_t *r,
17 ngx_http_upstream_t *u); 17 ngx_http_upstream_t *u);
18 static ngx_int_t ngx_http_upstream_cache_status(ngx_http_request_t *r, 18 static ngx_int_t ngx_http_upstream_cache_status(ngx_http_request_t *r,
19 ngx_http_variable_value_t *v, uintptr_t data);
20 static ngx_int_t ngx_http_upstream_cache_last_modified(ngx_http_request_t *r,
19 ngx_http_variable_value_t *v, uintptr_t data); 21 ngx_http_variable_value_t *v, uintptr_t data);
20 #endif 22 #endif
21 23
22 static void ngx_http_upstream_init_request(ngx_http_request_t *r); 24 static void ngx_http_upstream_init_request(ngx_http_request_t *r);
23 static void ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx); 25 static void ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx);
356 358
357 { ngx_string("upstream_cache_status"), NULL, 359 { ngx_string("upstream_cache_status"), NULL,
358 ngx_http_upstream_cache_status, 0, 360 ngx_http_upstream_cache_status, 0,
359 NGX_HTTP_VAR_NOCACHEABLE, 0 }, 361 NGX_HTTP_VAR_NOCACHEABLE, 0 },
360 362
363 { ngx_string("upstream_cache_last_modified"), NULL,
364 ngx_http_upstream_cache_last_modified, 0,
365 NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
366
361 #endif 367 #endif
362 368
363 { ngx_null_string, NULL, NULL, 0, 0, 0 } 369 { ngx_null_string, NULL, NULL, 0, 0, 0 }
364 }; 370 };
365 371
1804 } 1810 }
1805 1811
1806 #endif 1812 #endif
1807 } 1813 }
1808 1814
1815 #if (NGX_HTTP_CACHE)
1816
1817 if (status == NGX_HTTP_NOT_MODIFIED
1818 && u->cache_status == NGX_HTTP_CACHE_EXPIRED
1819 && u->conf->cache_revalidate)
1820 {
1821 time_t now, valid;
1822 ngx_int_t rc;
1823
1824 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1825 "http upstream not modified");
1826
1827 now = ngx_time();
1828 valid = r->cache->valid_sec;
1829
1830 rc = u->reinit_request(r);
1831
1832 if (rc != NGX_OK) {
1833 ngx_http_upstream_finalize_request(r, u, rc);
1834 return NGX_OK;
1835 }
1836
1837 u->cache_status = NGX_HTTP_CACHE_REVALIDATED;
1838 rc = ngx_http_upstream_cache_send(r, u);
1839
1840 if (valid == 0) {
1841 valid = r->cache->valid_sec;
1842 }
1843
1844 if (valid == 0) {
1845 valid = ngx_http_file_cache_valid(u->conf->cache_valid,
1846 u->headers_in.status_n);
1847 if (valid) {
1848 valid = now + valid;
1849 }
1850 }
1851
1852 if (valid) {
1853 r->cache->valid_sec = valid;
1854 r->cache->date = now;
1855
1856 ngx_http_file_cache_update_header(r);
1857 }
1858
1859 ngx_http_upstream_finalize_request(r, u, rc);
1860 return NGX_OK;
1861 }
1862
1863 #endif
1864
1809 return NGX_DECLINED; 1865 return NGX_DECLINED;
1810 } 1866 }
1811 1867
1812 1868
1813 static ngx_int_t 1869 static ngx_int_t
4490 v->data = ngx_http_cache_status[n].data; 4546 v->data = ngx_http_cache_status[n].data;
4491 4547
4492 return NGX_OK; 4548 return NGX_OK;
4493 } 4549 }
4494 4550
4551
4552 static ngx_int_t
4553 ngx_http_upstream_cache_last_modified(ngx_http_request_t *r,
4554 ngx_http_variable_value_t *v, uintptr_t data)
4555 {
4556 u_char *p;
4557
4558 if (!r->upstream->conf->cache_revalidate
4559 || r->upstream->cache_status != NGX_HTTP_CACHE_EXPIRED
4560 || r->cache->last_modified == -1)
4561 {
4562 v->not_found = 1;
4563 return NGX_OK;
4564 }
4565
4566 p = ngx_pnalloc(r->pool, sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1);
4567 if (p == NULL) {
4568 return NGX_ERROR;
4569 }
4570
4571 v->len = ngx_http_time(p, r->cache->last_modified) - p;
4572 v->valid = 1;
4573 v->no_cacheable = 0;
4574 v->not_found = 0;
4575 v->data = p;
4576
4577 return NGX_OK;
4578 }
4579
4495 #endif 4580 #endif
4496 4581
4497 4582
4498 static char * 4583 static char *
4499 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) 4584 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)