Caching static assets if present

Maxim Dounin mdounin at mdounin.ru
Wed Sep 3 04:55:10 MSD 2008


Hello!

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

>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?

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

location /static/ {
     ...
     error_page 404 = @fallback;
}

location @fallback {
     proxy_pass ...;
}

Maxim Dounin





More information about the nginx mailing list