# Caching static assets if present

**URL:** <https://www.ruby-forum.com/t/caching-static-assets-if-present/147548>\
**Category:** NGINX\
**Created:** [September 3, 2008, 1:42am UTC](https://www.ruby-forum.com/t/caching-static-assets-if-present/147548 "2008-09-03T01:42:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [September 3, 2008, 1:42am UTC](https://www.ruby-forum.com/t/caching-static-assets-if-present/147548/1 "2008-09-03T01:42:13Z")

</div>

Hello,

I am configuring an environment where at deployment-time static files  
are  
identified and stored in a known location. Also as part of deployment,  
each  
static file has a compressed version generated, and touch’ed with the  
same  
times as the source file (e.g. touch --reference=foo.js foo.js.gz).

The one interesting requirement that did not seem straight-forward is  
that I  
want to pass the URL to the back-end if the static file could not be  
found  
(instead of returning a 404). This should be a rare occurrence and can  
be  
tracked through the nginx logs.

What I have looks like:

location / {

# … various proxy settings

proxy\_pass [http://back-end](http://back-end) [http://zope-consumer/](http://zope-consumer/);  
}

location ^~ /static/ {  
alias /opt/nginx/assets/static/;

expires max;  
gzip\_static on;

error\_page 404 = /|dynamic|$uri;  
}

location ^~ /|dynamic|/ {  
internal;  
rewrite ^/[|]dynamic| $1;

# … various proxy settings

proxy\_pass [http://back-end](http://back-end) [http://zope-consumer](http://zope-consumer);  
}

Is there a cleaner way to do this?

Thanks,

---

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [September 3, 2008, 7:33am UTC](https://www.ruby-forum.com/t/caching-static-assets-if-present/147548/2 "2008-09-03T07:33:40Z")

</div>

Hello,

On Tue, Sep 2, 2008 at 5:55 PM, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello!

> }
> 
> Maxim D.

Great, thank you for the pointer. Is this location feature documented  
anywhere on the wiki?

---

<div class="post-metadata">

**Author:** ![archfiend997](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@archfiend997](https://www.ruby-forum.com/u/archfiend997)\
**Post date:** [September 3, 2008, 3:04am UTC](https://www.ruby-forum.com/t/caching-static-assets-if-present/147548/3 "2008-09-03T03:04:05Z")

</div>

Hello!

On Tue, Sep 02, 2008 at 04:03:34PM -0700, Adam Z. wrote:

> tracked through the nginx logs.  
> alias /opt/nginx/assets/static/;
> 
> # … various proxy settings
> 
> proxy\_pass [http://back-end](http://back-end) [http://zope-consumer](http://zope-consumer);  
> }
> 
> Is there a cleaner way to do this?

Named locations was introduced specially for such things. Try the  
following:

location /static/ {  
…  
error\_page 404 = @fallback;  
}

location @fallback {  
proxy\_pass …;  
}

Maxim D.
