# Nginx cache files by mime type using ngx\_srcache module

**URL:** <https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283>\
**Category:** NGINX\
**Created:** [June 11, 2013, 11:11am UTC](https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283 "2013-06-11T11:11:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [June 11, 2013, 11:11am UTC](https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283/1 "2013-06-11T11:11:12Z")

</div>

Hi,

I’m using ngx\_srcache module with Memcached to cache static contents of  
the  
upstream servers. This is based on static file extensions. Now I need to  
cached them by looking at Content-Type of the header to cache .html  
pages as  
we do not use the .html extension. (i.e.  
[http://www.example.com/this-is-my-site](http://www.example.com/this-is-my-site) )

Is this possible with ngx\_srcache module using map module or something  
similar…

http {  
map $upstream\_http\_content\_type $no\_proxy {  
default 0;  
~\*^html/ 0;  
}

and hook this $no\_proxy to ngx\_srcache variables like I’m doing with  
extension…?

```
location ~* \.(html)$ {
                    set $key $uri$args;
                    srcache_fetch GET /memc key=$key;
                    srcache_store PUT /memc 

```

key=$key&exptime=$ttl\_1m;  
proxy\_pass [http://www.example.com](http://www.example.com);  
}

Any help to this is really appreciated.

Thanks in advance.

Posted at Nginx Forum:

> **[Nginx cache files by mime type using ngx\_srcache module](https://forum.nginx.org/read.php?2,240023,240023#msg-240023)**
>
> Hi, I’m using ngx\_srcache module with Memcached to cache static contents of the upstream servers. This is based on static file extensions. Now I need to cached them by looking at Content-Type of the header to cache .html pages as we do not use the...

---

<div class="post-metadata">

**Author:** ![n1xman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@n1xman](https://www.ruby-forum.com/u/n1xman)\
**Post date:** [June 11, 2013, 9:06pm UTC](https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283/2 "2013-06-11T21:06:03Z")

</div>

Hello!

On Tue, Jun 11, 2013 at 2:10 AM, n1xman wrote:

> default 0;  
> ~\*^html/ 0;  
> }
> 
> and hook this $no\_proxy to ngx\_srcache variables like I’m doing with  
> extension…?
> 
> ```
> location ~* \.(html)$ {
> set $key $uri$args;
> 
> ```

Just a side note: because you’re using the key in the “key” URI  
argument below, you should escape it because the value may contain  
special characters. That is,

```
set_escape_uri $key $uri$args;

```

And then in your location = /memc, you should unescape the “key” URI  
argument like this:

```
set_unescape_uri $key $arg_key;

```

> ```
> srcache_fetch GET /memc key=$key;
> srcache_store PUT /memc key=$key&exptime=$ttl_1m;
> proxy_pass http://www.example.com;
> }
> 
> ```

You can use the srcache\_store\_skip directive to achieve this:

```
http://wiki.nginx.org/HttpSRCacheModule#srcache_store_skip

```

Basically, you can do something like this:

```
map $upstream_http_content_type $no_store {
    default 1;
    ~*html 0;
}

server {
    ...
    location / {
       set_escape_uri $key $uri$args;
       srcache_fetch GET /memc key=$key;
       srcache_store PUT /memc key=$key&exptime=$ttl_1m;
       proxy_pass http://www.example.com;

       srcache_store_skip $no_store;
   }
}

```

Best regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![n1xman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@n1xman](https://www.ruby-forum.com/u/n1xman)\
**Post date:** [June 12, 2013, 12:51pm UTC](https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283/3 "2013-06-12T12:51:57Z")

</div>

Hi agentzh,

Thanks for the support and it is working 🙂

However, I have noticed every srcache\_fetch GET /memc subrequest hits  
the  
memcached even though srcache\_store skipped content-type which is not  
defined.

This will keep busy the memcached server and I think srcache\_fetch GET  
subrequest should not executed if srcache\_store\_skip triggered first. Or  
am  
I missing something here…

map $upstream\_http\_content\_type $no\_store {  
default 1;  
~\*html 0;  
}

server {  
…

```
location /memc {
            internal;
            set_unescape_uri $memc_key $arg_key;
            set $memc_key $arg_key;
            set $memc_exptime $arg_exptime;
            memc_pass 127.0.0.1:11211;
}

            location / {
                    srcache_response_cache_control off;
                    set_escape_uri $key $uri$args;
                    set $ttl_1d 60;
                    srcache_fetch GET /memc key=$key;
                    srcache_store PUT /memc 

```

key=$key&exptime=$ttl\_1d;  
srcache\_store\_statuses 200;  
proxy\_pass [http://192.168.0.61:900](http://192.168.0.61:900);  
srcache\_store\_skip $no\_store;

```
            }

```

…  
}

Find the debug logs when firing following urls

curl -i [http://192.168.0.160/index.html](http://192.168.0.160/index.html) \< served from the memcached  
curl -i [http://192.168.0.160/santa.jpg](http://192.168.0.160/santa.jpg) \< not served from the memcached  
but  
the request hits the memcached

error.log  
[http://pastebin.com/jbMm83qh](http://pastebin.com/jbMm83qh)

[root@localhost conf.d]# nginx -V  
nginx version: nginx/1.4.1  
built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)  
TLS SNI support enabled  
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx  
–conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid  
–lock-path=/var/run/nginx.lock  
–http-client-body-temp-path=/var/cache/nginx/client\_temp  
–http-proxy-temp-path=/var/cache/nginx/proxy\_temp  
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi\_temp  
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi\_temp  
–http-scgi-temp-path=/var/cache/nginx/scgi\_temp --user=nginx  
–group=nginx  
–with-http\_ssl\_module --with-http\_realip\_module  
–with-http\_addition\_module  
–with-http\_sub\_module --with-http\_dav\_module --with-http\_gunzip\_module  
–with-http\_gzip\_static\_module --with-http\_random\_index\_module  
–with-http\_secure\_link\_module --with-http\_stub\_status\_module  
–with-mail\_ssl\_module --with-file-aio --without-mail\_smtp\_module  
–without-mail\_imap\_module --without-mail\_pop3\_module --with-debug  
–with-http\_spdy\_module  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/ngx\_devel\_kit-master  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/echo-nginx-module-master  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/set-misc-nginx-module-master  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/srcache-nginx-module-master  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/nginx-sticky-module-1.1  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/nginx\_upstream\_check\_module-master  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/memc-nginx-module-master  
–add-module=/usr/local/hirantha/rpmbuild/BUILD/nginx-1.4.1/contrib/nginx\_cross\_origin\_module-master  
–with-cc-opt=‘-O2 -g’

Really appreciate your support here.

Posted at Nginx Forum:

> **[Re: Nginx cache files by mime type using ngx\_srcache module](https://forum.nginx.org/read.php?2,240023,240049#msg-240049)**
>
> Hi, I’m using ngx\_srcache module with Memcached to cache static contents of the upstream servers. This is based on static file extensions. Now I need to cached them by looking at Content-Type of the header to cache .html pages as we do not use the...

---

<div class="post-metadata">

**Author:** ![n1xman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@n1xman](https://www.ruby-forum.com/u/n1xman)\
**Post date:** [June 18, 2013, 5:37pm UTC](https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283/4 "2013-06-18T17:37:49Z")

</div>

Hi agentzh,

Thanks for the response. It is not happening on my uat site as I have  
redone  
the configuration and tested with the full setup. However it happened on  
my  
try out setup where I did not want image/\* content-type to be cached.

2013/06/12 15:51:43 [info] 11016#0: \*19 key: “%252fsanta.jpg” was not  
found  
by memcached while reading response header from upstream, client:  
192.168.0.61, server: -, request: “GET /santa.jpg HTTP/1.1”, subrequest:  
“/memc”

Maybe my configuration problem ☹

Thanks again for the support and the setup is now cached by MIME types  
also;  
we are running ngx\_srcache without a problem. 🙂

Posted at Nginx Forum:

> **[Re: Nginx cache files by mime type using ngx\_srcache module](https://forum.nginx.org/read.php?2,240023,240195#msg-240195)**
>
> Hi, I’m using ngx\_srcache module with Memcached to cache static contents of the upstream servers. This is based on static file extensions. Now I need to cached them by looking at Content-Type of the header to cache .html pages as we do not use the...

---

<div class="post-metadata">

**Author:** ![n1xman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@n1xman](https://www.ruby-forum.com/u/n1xman)\
**Post date:** [June 13, 2013, 9:39pm UTC](https://www.ruby-forum.com/t/nginx-cache-files-by-mime-type-using-ngx-srcache-module/235283/5 "2013-06-13T21:39:24Z")

</div>

Hello!

On Wed, Jun 12, 2013 at 3:51 AM, n1xman wrote:

> However, I have noticed every srcache\_fetch GET /memc subrequest hits the  
> memcached even though srcache\_store skipped content-type which is not  
> defined.

The cache fetch operation must be performed _before_ sending a request  
to the backend while the Content-Type response header is only  
available _after_ the backend request is already sent. You’re having a  
chicken and egg problem here. And I don’t see how the srcache\_fetch  
can be skipped without a Content-Type response header available for  
checks.

> This will keep busy the memcached server and I think srcache\_fetch GET  
> subrequest should not executed if srcache\_store\_skip triggered first.

Are you sure?

srcache\_store\_skip always runs after srcache\_fetch for a particular  
request. The caching workflow looks like this:

```
srcache_fetch
send backend request if it's a cache miss
receive backend response (headers)
srcache_store

```

I cannot see how srcache\_store\_skip can be triggered before  
srcache\_fetch without sending a backend request to get the  
Content-Type response header.

Best regards,  
-agentzh
