# Return file when it's in cache/check if file exists in cache

**URL:** <https://www.ruby-forum.com/t/return-file-when-its-in-cache-check-if-file-exists-in-cache/236433>\
**Category:** NGINX\
**Created:** [September 3, 2013, 2:52pm UTC](https://www.ruby-forum.com/t/return-file-when-its-in-cache-check-if-file-exists-in-cache/236433 "2013-09-03T14:52:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 3, 2013, 2:52pm UTC](https://www.ruby-forum.com/t/return-file-when-its-in-cache-check-if-file-exists-in-cache/236433/1 "2013-09-03T14:52:53Z")

</div>

I’m using ngnix as proxy server. There is a situation when backend  
server is  
down and I know about it. Thus I’m adding ‘Check-Cache’ header to  
request  
and what I want to do is get the file when it is in cache, and when it  
is  
not just return error page. I don’t want to pass the request to the  
backend.

Scenario

$ curl -s -o /dev/null -w “%{http\_code}” 0/images/2.jpg -H’Host:  
[example.com](http://example.com)’  
200  
$ pkill -9 backend\_server  
$ curl -s -o /dev/null -w “%{http\_code}” 0/images/2.jpg -H’Host:  
[example.com](http://example.com)’ -H’Check-Cache: true’  
404 ← I want to 200 here

nginx.conf

server {  
listen 80;

```
underscores_in_headers on;
proxy_cache_purge on;

location / {
    error_page 599 = @jail;
    recursive_error_pages on;

    if ($http_check_cache = "true") {
        return 599;
    }

    proxy_pass http://localhost:8080;
    proxy_set_header Host $host;
    proxy_cache my-cache;
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
    proxy_cache_key $uri$is_args$args;
}

location @jail {
    # try_files ??
    # proxy_cache_key (If i can get it i can use try_files)
    # what other solution...
    return 404;
}

```

}

Posted at Nginx Forum:

> **[Return file when it's in cache/check if file exists in cache](https://forum.nginx.org/read.php?2,242488,242488#msg-242488)**
>
> I'm using ngnix as proxy server. There is a situation when backend server is down and I know about it. Thus I'm adding 'Check-Cache' header to request and what I want to do is get the file when it is in cache, and when it is not just return error...

---

<div class="post-metadata">

**Author:** ![ixos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ixos](https://www.ruby-forum.com/u/ixos)\
**Post date:** [September 6, 2013, 10:49am UTC](https://www.ruby-forum.com/t/return-file-when-its-in-cache-check-if-file-exists-in-cache/236433/2 "2013-09-06T10:49:03Z")

</div>

Ok, maybe not so beautiful solution but upstream can be used with one  
server  
makred as down.

http {  
upstream backend-jail {  
server 0.0.0.0 down;  
}

server {  
listen 80;

```
underscores_in_headers on;

recursive_error_pages on;
error_page 597 = @jail;

location / {
    if ($http_x_backend_down = "1") {
        return 597;
    }

    proxy_pass http://lcoalhost:8080;
    proxy_set_header Host $host;

    proxy_cache my-cache;
    proxy_cache_valid 200 302 1h;
    proxy_cache_valid 404 1m;
    proxy_cache_key $uri$is_args$args;
}

location @jail {
    # dont need to log error about 'no live upstreams'
    error_log /dev/null crit;

    # backend-jail always return 502, want 404 to be returned.
    error_page 502 =404 /;

    proxy_pass http://backend-jail;
}

```

}  
}

Posted at Nginx Forum:

> **[Re: Return file when it's in cache/check if file exists in cache](https://forum.nginx.org/read.php?2,242488,242618#msg-242618)**
>
> I'm using ngnix as proxy server. There is a situation when backend server is down and I know about it. Thus I'm adding 'Check-Cache' header to request and what I want to do is get the file when it is in cache, and when it is not just return error...
