Ruby Forum NGINX > Caching static assets if present

Posted by Adam Zell (Guest)
on 03.09.2008 01:42
(Received via mailing list)
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://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://zope-consumer>;
}

Is there a cleaner way to do this?

Thanks,
Posted by Maxim Dounin (Guest)
on 03.09.2008 03:04
(Received via mailing list)
Hello!

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

>tracked through the nginx logs.
>  alias /opt/nginx/assets/static/;
>
>  # ... various proxy settings
>
>  proxy_pass http://back-end <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 Dounin
Posted by Adam Zell (Guest)
on 03.09.2008 07:33
(Received via mailing list)
Hello,

On Tue, Sep 2, 2008 at 5:55 PM, Maxim Dounin <mdounin@mdounin.ru> wrote:

> Hello!
> <snip>
>


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