# Caching needed, but makes kitty sad

**URL:** <https://www.ruby-forum.com/t/caching-needed-but-makes-kitty-sad/170168>\
**Category:** NGINX\
**Created:** [June 13, 2009, 4:12am UTC](https://www.ruby-forum.com/t/caching-needed-but-makes-kitty-sad/170168 "2009-06-13T04:12:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jessica\_Hawkwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@Jessica\_Hawkwell](https://www.ruby-forum.com/u/Jessica_Hawkwell)\
**Post date:** [June 13, 2009, 4:12am UTC](https://www.ruby-forum.com/t/caching-needed-but-makes-kitty-sad/170168/1 "2009-06-13T04:12:51Z")

</div>

Hai peeps! =^\_^=

I have a system that stores a lot of images in a database. What I’m  
running is an avatar site, and there are times when it seems everyone  
has decided to update their avatars all at the same time. With nearly  
70% of our non-static image requests being little 32x32 icons for all  
the items that can be equipped, mass amounts of icon loads sometimes  
makes the site run about a second or two slower than usual.

I came to the obvious conclusion the best way to resolve this is to have  
nginx cache these little item icons. Unfortunately, I have tried and  
failed to make this happen. I think having nginx caching them would  
greatly improve performance since it would not have to access the  
back-end FastCGI servers to get the icons, they’d be local (which is  
always faster, so yay). Below is the server block in question, I  
apologize in advance if the regex for /avatars/ causes any negative side  
effects.

```
    server {
            listen 80;
            server_name varusonline.com;
            root /usr/local/www/varusonline.com/;

            location / {
                    gzip on;
                    gzip_types image/png image/gif image/jpeg

```

text/plain text/javascript text/css;  
}

```
            location /avatars/ {
                    rewrite

```

^/avatars/([-0-9]+)(|\_head|\_full).(png|gif|jpg)(?(.\*))?$  
/avatar.php?u=$1&show=$2&format=$3&$5 last;  
}

```
            location /domains/ {
                    rewrite

```

^/domains/([a-z]+)/([-0-9a-z.,]+)/([-0-9a-z.]+)?(?(.\*))?$  
/domains.php?$1=$2&item=$3&$5 last;  
}

```
            location /img_view/ {
                    set $img $document_uri;
                    rewrite ^/img_view/(avi|bod)-([0-9]+).png$

```

/img\_view.php?subset=$1&img\_id=$2 last;  
set $droot /usr/local/www/caches/img\_view/;

```
                    proxy_store $droot${img};

                    if (!-f $droot${img}) {
                            fastcgi_pass fcgi;
                    }

                    if (-f $droot${img}) {
                            expires max;
                    }
            }

            location ~ \.php$ {
                    fastcgi_pass fcgi;
            }
    }

```

Through all of this with everything I have tried, the cache folder is  
still empty, which makes this little kitty sad. Is there a way to make  
nginx cache images from just that one script?

Tanks, Jessica. =^\_^=

* * *

Owner, Varus Online  
[http://varusonline.com/](http://varusonline.com/)  
Member Support: 530 383 3059

— Useful Links —  
Frequently Asked Questions:  
[http://varusonline.com/forum/viewtopic.php?t=461](http://varusonline.com/forum/viewtopic.php?t=461)  
Newbie Guide: [http://varusonline.com/forum/viewtopic.php?t=7430](http://varusonline.com/forum/viewtopic.php?t=7430)  
Useful Tips: [http://varusonline.com/forum/viewtopic.php?t=6803](http://varusonline.com/forum/viewtopic.php?t=6803)

When taking over the world, use zombie squirrels.  
Nobody will ever see it coming. \>.\>

---

<div class="post-metadata">

**Author:** ![Jessica\_Hawkwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@Jessica\_Hawkwell](https://www.ruby-forum.com/u/Jessica_Hawkwell)\
**Post date:** [June 20, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/caching-needed-but-makes-kitty-sad/170168/2 "2009-06-20T18:54:19Z")

</div>

On Jun 12, Jessica Hawkwell wrote:

> nginx cache these little item icons. Unfortunately, I have tried and
> 
> ```
> }
> 
> ```
> 
> /img\_view.php?subset=$1&img\_id=$2 last;  
> }  
> }
> 
> ```
> location ~ \.php$ {
> fastcgi_pass fcgi;
> }
> }
> 
> ```
> 
> Through all of this with everything I have tried, the cache folder is still  
> empty, which makes this little kitty sad. Is there a way to make nginx  
> cache images from just that one script?

Is your php script returning the appropriate cache control headers?

---

<div class="post-metadata">

**Author:** ![Jessica\_Hawkwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@Jessica\_Hawkwell](https://www.ruby-forum.com/u/Jessica_Hawkwell)\
**Post date:** [June 20, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/caching-needed-but-makes-kitty-sad/170168/3 "2009-06-20T19:30:38Z")

</div>

Arvind Jayaprakash wrote:

> > ```
> > server_name varusonline.com;
> > 
> > ```
> > 
> > ^/avatars/([-0-9]+)(|\_head|\_full).(png|gif|jpg)(?(.\*))?$  
> > set $img $document\_uri;  
> > if (-f $droot${img}) {  
> > empty, which makes this little kitty sad. Is there a way to make nginx  
> > cache images from just that one script?
> 
> Is your php script returning the appropriate cache control headers?

The PHP script returns a 60 second cache control header, which seems to  
have no effect on nginx. I’ve got a solution in place for the item  
icons (FastCGI data store). The big one now is having nginx cache the  
actual avatars, which proves to be difficult.

* * *

Owner, Varus Online  
[http://varusonline.com/](http://varusonline.com/)  
Member Support: 530 383 3059

— Useful Links —  
Frequently Asked Questions:  
[http://varusonline.com/forum/viewtopic.php?t=461](http://varusonline.com/forum/viewtopic.php?t=461)  
Newbie Guide: [http://varusonline.com/forum/viewtopic.php?t=7430](http://varusonline.com/forum/viewtopic.php?t=7430)  
Useful Tips: [http://varusonline.com/forum/viewtopic.php?t=6803](http://varusonline.com/forum/viewtopic.php?t=6803)

When taking over the world, use zombie squirrels.  
Nobody will ever see it coming. \>.\>
