Welcome! Log In Create A New Profile

Advanced

Re: cache gzipped content with proxy_pass

August 21, 2010 07:27AM
Maxim,

thank you for your answer. I've patched nginx 0.7.67 to export gzip_ok to config file variable space (see attachement). I'm not a C guy, but it seems to work :-) I' currently using it like this:

location /json-data/ {
set $ae "";
if ($gzip_ok) {
set $ae "gzip";
}
proxy_pass http://10.2.3.4/json-upstream/;
proxy_cache_key "$gzip_ok:$uri";
proxy_set_header User-Agent "nginx";
proxy_set_header Accept-Encoding $ae;
[ ... ]
}

I get cache keys like this:

# grep -a ^KEY /var/spool/nginx/*
/var/spool/nginx/0c341794ed57bf03d72fb9a207f43d0e:KEY: :/json-data/123/45/
/var/spool/nginx/17961a45b339647f5e8f239dde713428:KEY: OK:/json-data/123/45/

By hiding the client User-Agent and Accept-Encoding, I deactivate upstream gzip detection logic. For this specific task this patch/config works perfectly. I more general approach would be nice, though.

Chris



--- src/http/ngx_http_variables.c.orig 2010-08-21 12:17:42.000000000 +0200
+++ src/http/ngx_http_variables.c 2010-08-21 12:19:07.000000000 +0200
@@ -66,6 +66,10 @@
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_request_completion(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+#if (NGX_HTTP_GZIP)
+static ngx_int_t ngx_http_variable_gzip_ok(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+#endif
static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r,
@@ -123,6 +127,9 @@
#if (NGX_HTTP_GZIP)
{ ngx_string("http_via"), NULL, ngx_http_variable_header,
offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
+
+ { ngx_string("gzip_ok"), NULL, ngx_http_variable_gzip_ok,
+ 0, 0, 0 },
#endif

#if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
@@ -1538,6 +1545,41 @@
return NGX_OK;
}

+#if (NGX_HTTP_GZIP)
+static ngx_int_t
+ngx_http_variable_gzip_ok(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_int_t rc;
+
+ if (!r->gzip_tested) {
+ rc = ngx_http_gzip_ok(r);
+
+ if (rc != NGX_OK && rc != NGX_DECLINED) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+ }
+
+ if (r->gzip_ok) {
+ v->len = 2;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = (u_char *) "OK";
+
+ return NGX_OK;
+ }
+
+ v->len = 0;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = (u_char *) "";
+
+ return NGX_OK;
+}
+#endif

static ngx_int_t
ngx_http_variable_request_body(ngx_http_request_t *r,
Subject Author Posted

cache gzipped content with proxy_pass

chrisl August 16, 2010 08:56AM

Re: cache gzipped content with proxy_pass

Maxim Dounin August 16, 2010 10:52AM

Re: cache gzipped content with proxy_pass

chrisl August 21, 2010 07:27AM

Re: cache gzipped content with proxy_pass

adamchal May 26, 2011 08:28PM

Re: cache gzipped content with proxy_pass

SplitIce May 26, 2011 11:50PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 102
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready