# Cache by mime type

**URL:** <https://www.ruby-forum.com/t/cache-by-mime-type/221590>\
**Category:** NGINX\
**Created:** [July 20, 2012, 3:57pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590 "2012-07-20T15:57:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 20, 2012, 3:57pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/1 "2012-07-20T15:57:20Z")

</div>

Like the title says, can I cache images (or whatever) by mime type, not  
extension? The only relevant info I found is at

> <https://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type>

However, the suggested config is not valid, because proxy directive  
cannot be used inside if.

Posted at Nginx Forum:

> **[Cache by mime type](https://forum.nginx.org/read.php?2,228786,228786#msg-228786)**
>
> Like the title says, can I cache images (or whatever) by mime type, not extension? The only relevant info I found is at http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type However, the suggested config is not valid, because proxy...

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 4:03pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/2 "2012-07-20T16:03:46Z")

</div>

20.07.2012, 20:57, “burn” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Like the title says, can I cache images (or whatever) by mime type, not  
> extension?

Yes.

> **[Module ngx\_http\_map\_module](http://nginx.org/en/docs/http/ngx_http_map_module.html)**

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

> The only relevant info I found is at  
> [http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type](http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type)  
> However, the suggested config is not valid, because proxy directive  
> cannot be used inside if.

forget about “if”.

–  
br, Denis F. Latypoff.

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 7:48pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/3 "2012-07-20T19:48:11Z")

</div>

can you elaborate on how to implement it? For me, it seems to cache html  
as well (I’m checking saved files). Here’s what I come up with:

nginx.conf

user nginx;  
worker\_processes 8;  
worker\_rlimit\_nofile 100000;  
error\_log /var/log/nginx/error.log;  
pid /var/run/nginx.pid;  
events {  
worker\_connections 1024;  
use epoll;  
}  
http {  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;  
log\_format main '$remote\_addr - $remote\_user [$time\_local]  
“$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;  
access\_log /var/log/nginx/access.log combined;  
sendfile on;  
tcp\_nopush on;  
tcp\_nodelay on;  
server\_tokens off;  
gzip on;  
gzip\_static on;  
gzip\_comp\_level 5;  
gzip\_min\_length 1024;  
keepalive\_timeout 65;  
limit\_conn\_zone $binary\_remote\_addr zone=addr:10m;  
map $http\_content\_type $no\_proxy {  
default 0;  
"~_image/" 1;  
}  
proxy\_cache\_path /tmp/nginx/cache/image levels=1:1:2  
keys\_zone=image:1000m;  
proxy\_temp\_path /tmp/nginx/temp;  
include /etc/nginx/conf.d/_.conf;  
include /etc/nginx/vhosts/\*;  
}

vhost:

server {  
listen 8082;  
server\_name .forum.domain.com;  
charset utf-8;  
access\_log /var/log/nginx/forum.domain.com-access.log2 combined;  
error\_log /var/log/nginx/forum.domain.com-error.log2;  
location / {  
root /var/www/html/forum.domain.com;  
index index.php;  
try\_files $uri @apache;  
}  
location /forum {  
root /var/www/html/forum.domain.com;  
index index.php;  
try\_files $uri @apache;  
}  
location /gallery {  
root /var/www/html/forum.domain.com;  
index index.php;  
try\_files $uri @apache;  
}  
location ~ ^.\*.php$ {  
proxy\_pass [http://127.0.0.1:8081](http://127.0.0.1:8081);  
include proxy.cache.inc;  
}  
location @apache {  
proxy\_pass [http://127.0.0.1:8081](http://127.0.0.1:8081);  
include proxy.cache.inc;  
}  
}

proxy.cache.inc:

proxy\_cache image;  
proxy\_cache\_use\_stale updating;  
proxy\_cache\_valid 7d;  
proxy\_cache\_bypass $no\_proxy;  
proxy\_connect\_timeout 59s;  
proxy\_send\_timeout 600;  
proxy\_read\_timeout 600;  
proxy\_buffer\_size 64k;  
proxy\_buffers 16 32k;  
proxy\_busy\_buffers\_size 64k;  
proxy\_temp\_file\_write\_size 64k;  
proxy\_pass\_header Set-Cookie;  
proxy\_redirect off;  
proxy\_hide\_header Vary;  
proxy\_set\_header Accept-Encoding ‘’;  
proxy\_ignore\_headers Cache-Control Expires;  
proxy\_set\_header Referer $http\_referer;  
proxy\_set\_header Host $host;  
proxy\_set\_header Cookie $http\_cookie;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-Host $host;  
proxy\_set\_header X-Forwarded-Server $host;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

Posted at Nginx Forum:

> **[Re: Cache by mime type](https://forum.nginx.org/read.php?2,228786,228794#msg-228794)**
>
> Like the title says, can I cache images (or whatever) by mime type, not extension? The only relevant info I found is at http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type However, the suggested config is not valid, because proxy...

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 8:23pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/4 "2012-07-20T20:23:49Z")

</div>

but I’m not using upstream directive. Do you mean that it’s only  
possible to upstream instead of direct proxy\_pass?  
I tried that. I still see non-image files in cache.

Posted at Nginx Forum:

> **[Re: Cache by mime type](https://forum.nginx.org/read.php?2,228786,228797#msg-228797)**
>
> Like the title says, can I cache images (or whatever) by mime type, not extension? The only relevant info I found is at http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type However, the suggested config is not valid, because proxy...

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 7:59pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/5 "2012-07-20T19:59:17Z")

</div>

On Friday 20 July 2012 21:47:35 burn wrote:  
[…]

> ```
> map $http_content_type $no_proxy {
> default 0;
> "~*image/" 1;
> }
> 
> ```

$upstream\_http\_content\_type

> **[Module ngx\_http\_upstream\_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables)**
>
> The ngx\_http\_upstream\_module module is used to define groups of servers that can be referenced by the proxy\_pass, fastcgi\_pass, uwsgi\_pass, scgi\_pass, memcached\_pass, and grpc\_pass directives.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 8:46pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/6 "2012-07-20T20:46:02Z")

</div>

with “proxy\_no\_cache $no\_proxy;”, there are css files in cache again.

Posted at Nginx Forum:

> **[Re: Cache by mime type](https://forum.nginx.org/read.php?2,228786,228800#msg-228800)**
>
> Like the title says, can I cache images (or whatever) by mime type, not extension? The only relevant info I found is at http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type However, the suggested config is not valid, because proxy...

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 8:51pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/7 "2012-07-20T20:51:18Z")

</div>

On Friday 20 July 2012 22:45:44 burn wrote:

> with “proxy\_no\_cache $no\_proxy;”, there are css files in cache again.

If you configure $no\_proxy like this:

default 0;  
“~\*image/” 1;

Images will not be cached. All others will.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 8:32pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/8 "2012-07-20T20:32:00Z")

</div>

On Friday 20 July 2012 22:23:26 burn wrote:

> but I’m not using upstream directive. Do you mean that it’s only  
> possible to upstream instead of direct proxy\_pass?

Actually “proxy\_pass” uses upstream module regardless of whether you’re  
using the upstream directive or not.

> I tried that. I still see non-image files in cache.

Also you should use “proxy\_no\_cache” instead of “proxy\_cache\_bypass”.

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_no_cache)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 20, 2012, 9:05pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/9 "2012-07-20T21:05:54Z")

</div>

Right. The problem is that there were images as well. And now when I  
swapped values, it seems that nothing at all is cached.

Posted at Nginx Forum:

> **[Re: Cache by mime type](https://forum.nginx.org/read.php?2,228786,228802#msg-228802)**
>
> Like the title says, can I cache images (or whatever) by mime type, not extension? The only relevant info I found is at http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type However, the suggested config is not valid, because proxy...

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 22, 2012, 10:09am UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/10 "2012-07-22T10:09:53Z")

</div>

On Fri, Jul 20, 2012 at 02:45:44PM -0400, burn wrote:

Hi there,

> with “proxy\_no\_cache $no\_proxy;”, there are css files in cache again.

I think the issue is with using both proxy\_no\_cache and  
proxy\_cache\_bypass  
with the same $no\_proxy variable.

(I think there was a bug related to this in the past – something about  
if you didn’t use them with the same variable, bad things could happen.)

proxy\_cache\_bypass takes effect before the upstream is  
contacted, so at that point $no\_proxy is set based on an empty  
$upstream\_http\_content\_type. Later on, when proxy\_no\_cache is  
considered,  
$no\_proxy already has a value so that is used.

The following config seems to work for me:

===  
http {  
map $upstream\_http\_content\_type $no\_proxy {  
default proxy;  
~\*^image/ 0;  
}  
proxy\_cache\_path proxy\_cache keys\_zone=images:1m inactive=1m;

```
server {
    location / {
        proxy_pass http://127.0.0.1:10080/;
        proxy_no_cache $no_proxy;
        proxy_cache_bypass 0;
        proxy_cache images;
    }
}

```

# }

I can run the two commands

curl -i [http://localhost:8000/a.png](http://localhost:8000/a.png)  
curl -i [http://localhost:8000/a.txt](http://localhost:8000/a.txt)

and I see exactly one file in my proxy\_cache directory. (And the error  
log says what is going on.)

And then I can run the commands again, and from the Expires header,  
I can see that the txt file came from upstream, and the png file came  
from cache.

(I use the proxy\_cache\_bypass line just to hush a warning (in 1.2.1). I  
have _not_ tested this for the partly-remembered bug mentioned above.)

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 22, 2012, 11:28am UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/11 "2012-07-22T11:28:09Z")

</div>

Thanks, that seems to work. But I still don’t quite understand how. I  
did use both directives with the same variable. Now when I set  
“proxy\_cache\_bypass 0;”, it seems to correctly cache images and serve  
that cache. I’d imagine that unconditional 0 in that directive would  
cause nothing being served from cache ever (as that variable is  
considered first). By the way, my Nginx version is 1.3.1.

Posted at Nginx Forum:

> **[Re: Cache by mime type](https://forum.nginx.org/read.php?2,228786,228836#msg-228836)**
>
> Like the title says, can I cache images (or whatever) by mime type, not extension? The only relevant info I found is at http://stackoverflow.com/questions/6212361/nginx-cache-file-by-mime-type However, the suggested config is not valid, because proxy...

---

<div class="post-metadata">

**Author:** ![burn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@burn](https://www.ruby-forum.com/u/burn)\
**Post date:** [July 22, 2012, 8:12pm UTC](https://www.ruby-forum.com/t/cache-by-mime-type/221590/12 "2012-07-22T20:12:45Z")

</div>

On Sun, Jul 22, 2012 at 05:27:44AM -0400, burn wrote:

Hi there,

> Thanks, that seems to work. But I still don’t quite understand how. I  
> did use both directives with the same variable. Now when I set  
> “proxy\_cache\_bypass 0;”, it seems to correctly cache images and serve  
> that cache.

proxy\_cache\_bypass is “shall I serve this from upstream, even if I  
already have a valid cached version”. Setting it to 0 (or empty) means  
that everything that is valid in the cache, will be served from the  
cache.

Strictly, it shouldn’t be used in this case at all. But it is needed  
to hush the warning that is presumably due to the bug mentioned in

> **[proxy\_cache doesn't return headers](http://mailman.nginx.org/pipermail/nginx/2011-March/025557.html)**

(I think that that bug was fixed in 1.0.3; possibly the warning is no  
longer needed?)

And now you are using proxy\_cache\_bypass without an identical  
proxy\_no\_cache – although you’re not actually caring about the  
proxy\_cache\_bypass part, so maybe you won’t have the problems referred  
to.

> I’d imagine that unconditional 0 in that directive would  
> cause nothing being served from cache ever (as that variable is  
> considered first).

No, unconditional 0 there means “serve from cache if possible”, and is  
equivalent to the line being omitted.

proxy\_no\_cache is about “shall I actually save this response into the  
cache, even though all other indications are that I should do so”.  
That’s  
the one that you want to be “no, don’t” for all of the mime types that  
you don’t want cached.

All the best,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
