Nginx cache files by mime type using ngx_srcache module

agentzh agentzh at gmail.com
Tue Jun 11 19:05:31 UTC 2013


Hello!

On Tue, Jun 11, 2013 at 2:10 AM, n1xman wrote:
> 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 )
>
> 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;

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



More information about the nginx mailing list