Proxy_cache_key based on custom header

Peter Booth peter_booth at me.com
Fri Jun 30 17:29:49 UTC 2017


I had best caching experience when I started using the openresty nginx bundle. It's a build of nginx that contains a bunch of Lua modules that make it a lean application server. With that I could create cache keys that exactly matched my (complex) business requirements 

Sent from my iPhone

> On Jun 27, 2017, at 5:43 PM, deivid__ <nginx-forum at forum.nginx.org> wrote:
> 
> Hi.
> 
> I'm trying to use 2 level proxying to cache files delivered with
> X-Accel-Redirect. This kinda works, the only thing missing is getting the
> cache_key to be the filename.
> 
> 
> If `proxy_cache` is unset (or set to the default), the caching mechanism
> "works":
> - Every request gets cached (good)
> - Different URLs that map to the same file get mapped to different cache
> keys (bad)
> - I get the header "X-Banana" with the correct file path (good)
> 
> If `proxy_cache` is set to `$sent_http_x_test_header`:
> - Every request gets cached (good)
> - All URLs map to the same cache (very bad!)
> - I don't get the 'X-Banana' header at all
> 
> This leads me to believe that in the second case, `$sent_http_x_test_header`
> is empty. But it's not in the first case? Why? What can I do?
> 
> Full config below:
> 
> 
> proxy_cache_path /cache/nginx levels=1:2 keys_zone=cache:10m inactive=24h;
> 
> upstream backend {
>    server unix:///tmp/streaming-backend.sock;
> }
> 
> server {
>    listen 443 ssl;
>    listen [::]:443 ssl;
>    include /etc/nginx/ssl;
>    index index.html;
>    server_name pilotage.streamall.pw;
>    gzip off;
> 
>    proxy_cache_min_uses 1;
>    proxy_cache cache;
>    proxy_cache_valid 200 24h;
> 
>    location /v/ {
>        rewrite /v/(.+) /$1 break;
>        proxy_pass http://127.0.0.1:9999/;
> 
>        proxy_request_buffering off; # needs 1.7.11
>        proxy_ignore_headers X-Accel-Expires Expires Cache-Control
> Set-Cookie;
> 
>        # proxy_cache_key $sent_http_x_test_header;
>        add_header X-Proxy-Cache $upstream_cache_status;
>        add_header X-Banana $sent_http_x_test_header;
> 
>    }
> 
> }
> 
> server {
>    listen 9999;
>    location / {
>        uwsgi_pass backend;
>        include uwsgi_params;
> 
>        proxy_set_header   X-Real-IP       $remote_addr;
>        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
>    }
> 
>    location /converted/ {
>        internal;
>        root /nfs/;
>        add_header X-Test-Header $document_uri;
>    }
> }
> 
> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275178,275178#msg-275178
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx


More information about the nginx mailing list