# Nginx 1.9.12 proxy\_cache always returns MISS

**URL:** <https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306>\
**Category:** NGINX\
**Created:** [March 19, 2016, 9:10pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306 "2016-03-19T21:10:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [March 19, 2016, 9:10pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306/1 "2016-03-19T21:10:54Z")

</div>

Been playing with this for 2 days.

proxy\_pass is working correctly but the proxy\_cache\_path remains empty  
whatever I make.

Here’s the source I use for tests:  
root@NC-PH-0657-10:/etc/nginx/snippets# curl -X GET -I  
[http://www.kuriyama-truck.com/images/parts/13375/thumbnail\_0/1\_1.jpg](http://www.kuriyama-truck.com/images/parts/13375/thumbnail_0/1_1.jpg)  
HTTP/1.1 200 OK  
Date: Sat, 19 Mar 2016 18:15:16 GMT  
Server: Apache/2.4.16 (Amazon) PHP/5.6.17  
Last-Modified: Thu, 10 Mar 2016 05:01:30 GMT  
ETag: “d0f3-52daab51fbe80”  
Accept-Ranges: bytes  
Content-Length: 53491  
Content-Type: image/jpeg

Now here’s the response from the nginx:  
root@NC-PH-0657-10:/etc/nginx/snippets# curl -X GET -I  
[http://dev.ts-export.com/kuriyamacache/images/parts/13375/thumbnail\_0/1\_1.jpg](http://dev.ts-export.com/kuriyamacache/images/parts/13375/thumbnail_0/1_1.jpg)  
HTTP/1.1 200 OK  
Server: nginx  
Date: Sat, 19 Mar 2016 18:14:46 GMT  
Content-Type: image/jpeg  
Content-Length: 53491  
Connection: keep-alive  
Expires: Sun, 19 Mar 2017 18:14:46 GMT  
Cache-Control: max-age=31536000  
Cache-Control: public  
X-Cache-Status: MISS  
Accept-Ranges: bytes

Here are the request headers from my browser:  
GET /kuriyamacache/images/parts/13375/thumbnail\_1/1\_1.jpg HTTP/1.1  
Host: [dev.ts-export.com](http://dev.ts-export.com)  
Connection: keep-alive  
Cache-Control: max-age=0  
Accept:  
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,_/_;q=0.8  
Upgrade-Insecure-Requests: 1  
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36  
(KHTML,  
like Gecko) Chrome/49.0.2623.87 Safari/537.36  
DNT: 1  
Accept-Encoding: gzip, deflate, sdch  
Accept-Language: fr-CA,fr;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2,de;q=0.2  
Cookie: PRUM\_EPISODES=s=1458412951203

Part of my setup:

proxy\_cache\_path /tmp/nginx/dev levels=1:2 keys\_zone=my\_zone:10m  
max\_size=10g inactive=60m use\_temp\_path=off;

server {

set $rule\_3 0;  
set $rule\_4 0;  
set $rule\_5 0;  
set $rule\_8 0;  
set $rule\_9 0;

server\_name [dev.ts-export.com](http://dev.ts-export.com);  
listen 80;  
listen [::]:80;

root /home/tsuchi/public\_html;

if ($reject\_those\_args) {  
return 403;  
}

include snippets/filters.conf;

error\_page 404 /404.html;

if ($request\_uri ~ “^/index.(php|html?)$” ) {  
return 301 $scheme://dev.ts-export.com;  
}

# no SSL for IE6-8 on XP and Android 2.3

if ($scheme = https) {  
set $rule\_8 1$rule\_8;  
}  
if ($http\_user\_agent ~ “MSIE (6|7|8).\*Windows NT 5|Android 2.3”){  
set $rule\_8 2$rule\_8;  
}  
if ($rule\_8 = “210”){  
return 301 http://dev.ts-export.com$request\_uri;  
}

location = / {  
allow all;  
}

location = /robots.txt {  
add\_header X-Robots-Tag noindex;  
}

location ‘/.well-known/acme-challenge’ {  
default\_type “text/plain”;  
root /tmp/letsencrypt-auto;  
}

include snippets/proxyimg.conf;

location / {  
try\_files $uri $uri/ @rewrites;  
allow all;  
}  
(…)  
}

Contents of proxyimg.conf:

location ^~ /kuriyamacache {  
expires 1y;  
access\_log off;  
log\_not\_found off;  
resolver 127.0.0.1;

```
proxy_pass http://www.kuriyama-truck.com/;

proxy_cache my_zone;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_buffering on;

proxy_cache_valid 200 301 302 60m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout http_500 http_502 http_503

```

http\_504;  
proxy\_cache\_revalidate on;  
proxy\_cache\_lock on;

```
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_ignore_headers X-Accel-Redirect X-Accel-Expires 

```

X-Accel-Limit-Rate  
X-Accel-Buffering X-Accel-Charset Set-Cookie Cache-Control Vary Expires;

```
proxy_pass_header ETag;
proxy_hide_header Cache-Control;

add_header Cache-Control "public, max-age=31536000";
add_header X-Cache-Status $upstream_cache_status;

```

}

Posted at Nginx Forum:

> **[nginx 1.9.12 proxy\_cache always returns MISS](https://forum.nginx.org/read.php?2,265504,265504#msg-265504)**
>
> Been playing with this for 2 days. proxy\_pass is working correctly but the proxy\_cache\_path remains empty whatever I make. Here's the source I use for tests: root@NC-PH-0657-10:/etc/nginx/snippets# curl -X GET -I...

---

<div class="post-metadata">

**Author:** ![shiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shiz](https://www.ruby-forum.com/u/shiz)\
**Post date:** [March 19, 2016, 9:34pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306/2 "2016-03-19T21:34:17Z")

</div>

I think I’ve run into the problem before - move the proxypass statement  
from the top of the location stanza to the bottom, and I think that will  
solve your issue.

---

<div class="post-metadata">

**Author:** ![shiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shiz](https://www.ruby-forum.com/u/shiz)\
**Post date:** [March 19, 2016, 9:44pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306/3 "2016-03-19T21:44:13Z")

</div>

Seems like it’s resolved:

$ curl -I  
[http://dev.ts-export.com/kuriyamacache/images/parts/13375/thumbnail\_0/1\_1.jpg](http://dev.ts-export.com/kuriyamacache/images/parts/13375/thumbnail_0/1_1.jpg)  
HTTP/1.1 200 OK  
Server: nginx  
Date: Sat, 19 Mar 2016 20:42:46 GMT  
Content-Type: image/jpeg  
Content-Length: 53491  
Connection: keep-alive  
Last-Modified: Thu, 10 Mar 2016 05:01:30 GMT  
ETag: “d0f3-52daab51fbe80”  
Expires: Sun, 19 Mar 2017 20:42:46 GMT  
Cache-Control: max-age=31536000  
Cache-Control: public, max-age=31536000  
X-Cache-Status: MISS  
Accept-Ranges: bytes

$ curl -I  
[http://dev.ts-export.com/kuriyamacache/images/parts/13375/thumbnail\_0/1\_1.jpg](http://dev.ts-export.com/kuriyamacache/images/parts/13375/thumbnail_0/1_1.jpg)  
HTTP/1.1 200 OK  
Server: nginx  
Date: Sat, 19 Mar 2016 20:42:48 GMT  
Content-Type: image/jpeg  
Content-Length: 53491  
Connection: keep-alive  
Last-Modified: Thu, 10 Mar 2016 05:01:30 GMT  
ETag: “d0f3-52daab51fbe80”  
Expires: Sun, 19 Mar 2017 20:42:48 GMT  
Cache-Control: max-age=31536000  
Cache-Control: public, max-age=31536000  
X-Cache-Status: HIT  
Accept-Ranges: bytes

---

<div class="post-metadata">

**Author:** ![shiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shiz](https://www.ruby-forum.com/u/shiz)\
**Post date:** [March 19, 2016, 10:16pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306/4 "2016-03-19T22:16:56Z")

</div>

Although you stated you problem was resolved, you need to understand  
what  
you are configuring/testing.

In the first message you posted, the first response URL using the cache  
(starting with ‘/kuriyamacache’) did not correspond to the alleged  
request  
headers’ URL (‘thumbnail\_0’ vs ‘thumbnail\_1’), so you probably got mixed  
up  
in your tests.

Note that you configured cache only for path starting with  
‘/kuriyamacache’, not others, which will end up being served by another  
location block.  
Since you add headers in the response of requests being served at the  
proper location, look for their existence.

The cache will only get triggered if a requests is served by this  
specific  
location, if the request URI matches the cache key defined with the  
proxy\_cache\_key directive, if the status code of the response is  
cacheable,  
and with the cache entry being only valid for the specified amount of  
time.

## I do not thing the proxy\_pass directive is sensitive to order, otherwise you would not have seen the headers added with add\_header in any response. I encourage you to carefully re-test the configuration you provided here and if any trouble arise, please provide a detailed step-by-step way of reproducing the problem.

_B. R._

---

<div class="post-metadata">

**Author:** ![shiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shiz](https://www.ruby-forum.com/u/shiz)\
**Post date:** [March 19, 2016, 9:57pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306/5 "2016-03-19T21:57:04Z")

</div>

Great! =) Make sure proxy buffering stays on - that will bypass the  
cache  
if turned off, and make sure your key space is large because you’ll  
throw  
500s for everything if it runs out (I figured it would evict a key if it  
ran out of space, and what was a wrong assumption)

---

<div class="post-metadata">

**Author:** ![shiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shiz](https://www.ruby-forum.com/u/shiz)\
**Post date:** [March 19, 2016, 11:15pm UTC](https://www.ruby-forum.com/t/nginx-1-9-12-proxy-cache-always-returns-miss/244306/6 "2016-03-19T23:15:56Z")

</div>

Yes it’s resolved.

I’ve change the cache path yesterday and a few minutes ago, I noticed  
that  
error message:

“2016/03/19 12:31:02 [emerg] 8984#8984: cache “my\_zone” uses the  
“/tmp/nginx/dev” cache path while previously it used the “/tmp/nginx”  
cache  
path”

It seems it was enough to prevent the cache from running. Interesting.

BTW, do you see anything useless, redundant in my config?

Thanks for the help!

Posted at Nginx Forum:

> **[Re: nginx 1.9.12 proxy\_cache always returns MISS](https://forum.nginx.org/read.php?2,265504,265510#msg-265510)**
>
> Been playing with this for 2 days. proxy\_pass is working correctly but the proxy\_cache\_path remains empty whatever I make. Here's the source I use for tests: root@NC-PH-0657-10:/etc/nginx/snippets# curl -X GET -I...
