[PATCH] Proxy: Adding proxy_cache_key emedded variable

Thomas Peterson hidinginthebbc at gmail.com
Sat Nov 3 09:00:02 UTC 2018


# HG changeset patch
# User Thomas Peterson <hidinginthebbc at gmail.com>
# Date 1541231609 0
#      Sat Nov 03 07:53:29 2018 +0000
# Node ID 41a499230eb674b1b3ec7cfd093f3a074f9a0d09
# Parent  bddacdaaec9ef174504899f1528155f84bf60e59
Proxy: Adding proxy_cache_key emedded variable

This value being able to be set as part of response headers allows for greater
debugging of caching, and to permit analytics on cache-key distribution.

diff -r bddacdaaec9e -r 41a499230eb6 src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c	Wed Oct 31 16:49:40 2018 +0300
+++ b/src/http/modules/ngx_http_proxy_module.c	Sat Nov 03 07:53:29 2018 +0000
@@ -154,6 +154,8 @@
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_proxy_internal_chunked_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_proxy_cache_key_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
     ngx_table_elt_t *h, size_t prefix);
 static ngx_int_t ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r,
@@ -824,6 +826,11 @@
     { ngx_string("proxy_add_x_forwarded_for"), NULL,
       ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
 
+#if (NGX_HTTP_CACHE)
+    { ngx_string("proxy_cache_key"), NULL,
+      ngx_http_proxy_cache_key_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
+#endif
+
 #if 0
     { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
 #endif
@@ -2499,6 +2506,52 @@
     return NGX_OK;
 }
 
+static ngx_int_t
+ngx_http_proxy_cache_key_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    u_char              *p;
+    size_t             len;
+    ngx_str_t         *key;
+    ngx_uint_t           i;
+    ngx_http_cache_t    *c;
+    ngx_http_upstream_t *u;
+
+    u = r->upstream;
+    if(!u->cacheable) {
+        v->not_found = 1;
+        return NGX_ERROR;
+    }
+
+    c = r->cache;
+    len = 0;
+    key = c->keys.elts;
+    for (i = 0; i < c->keys.nelts; i++) {
+        len += key[i].len;
+    }
+    
+    if(len == 0) {
+        return NGX_ERROR;
+    }
+
+    p = ngx_pnalloc(r->pool, len);
+    if (p == NULL) {
+        return NGX_ERROR;
+    }
+    v->data = p;
+
+    i = 0;
+    for (i = 0; i < c->keys.nelts; i++) {
+        p = ngx_cpymem(p, key[i].data, key[i].len);
+    }
+
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->len = len;
+
+    return NGX_OK;
+}
 
 static ngx_int_t
 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,


More information about the nginx-devel mailing list