Proxy_cache_key based on custom header

deivid__ nginx-forum at
Tue Jun 27 21:43:22 UTC 2017


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
- 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;
    gzip off;

    proxy_cache_min_uses 1;
    proxy_cache cache;
    proxy_cache_valid 200 24h;

    location /v/ {
        rewrite /v/(.+) /$1 break;

        proxy_request_buffering off; # needs 1.7.11
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control

        # 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/ {
        root /nfs/;
        add_header X-Test-Header $document_uri;

Posted at Nginx Forum:,275178,275178#msg-275178

More information about the nginx mailing list