sending 404 responses for epty objects.

philipp nginx-forum at nginx.us
Mon May 4 11:52:46 UTC 2015


Hi Maxim,
should this solution work?
http://syshero.org/post/49594172838/avoid-caching-0-byte-files-on-nginx

I have created a simple test setup like:

map $upstream_http_content_length $flag_cache_empty {
  default 0;
  0 1;
}

server {
    listen       127.0.0.1:80;

    server_name   local;

    location /empty {
        return 200 "";
    }
    location /full {
        return 200 "full";
    }
}

server {
    listen       127.0.0.1:80;

    server_name   cache;

    location / {
        proxy_pass http://127.0.0.1;
        proxy_cache_valid 200 404 1h;
        proxy_no_cache $flag_cache_empty;
        proxy_cache_bypass $flag_cache_empty;
        proxy_set_header Host local;
        add_header X-Cache-Status $upstream_cache_status;
        add_header X-Cache-Empty $flag_cache_empty;
        add_header X-Upstream-Content-Length $upstream_http_content_length;
    }
}

But the flag is always 0:
vagrant at nginx-16-centos-64 bin]$ curl -v -H "Host: cache"
http://localhost/empty
* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /empty HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7
NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Accept: */*
> Host: cache
> 
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 04 May 2015 11:37:51 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< X-Cache-Status: MISS
< X-Cache-Empty: 0
< X-Upstream-Content-Length: 0
< 
* Connection #0 to host localhost left intact
* Closing connection #0

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,255421,258603#msg-258603



More information about the nginx mailing list