sending 404 responses for epty objects.

Maxim Dounin mdounin at mdounin.ru
Mon May 4 14:27:03 UTC 2015


Hello!

On Mon, May 04, 2015 at 07:52:46AM -0400, philipp wrote:

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

Removing proxy_cache_bypass should fix things for you.

The problem is that proxy_cache_bypass will be evaluated before a 
request is sent to upstream and therefore before 
$upstream_http_content_length will be available.  As a result 
$flag_cache_empty will be always 0.  And, because map results are 
cached for entire request lifetime, proxy_no_cache will see the 
same value, 0.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list