Use nginx as proxy with cache for satic files

Hi,

I want to use nginx as a reverse proxy to my backend server with a cache
for the static files.
Proxy and cache work perfectly, when I do it like this:

location / {
proxy_pass http://backend.server.org/;
proxy_cache cache;
proxy_cache_valid 200 10m;
}

However I don’t want to cache all the requests, only the requests on my
images folder.
So I tried this:

location / {
proxy_pass http://backend.server.org/;
}
location /images/ {
proxy_cache cache;
proxy_cache_valid 200 10m;
}

But now the image requests don’t go through the proxy anymore, resulting
in a 404 error. Is there any way to inherit the proxy settings from the
parent folder/a certain location?
Of course I could just add the proxy settings to the “location /images/”
block, but the images folder is not the only one I want to be cached, so
there has to be an easier way to do this.

In other words: I want all requests going through the proxy, but keep a
cache of ceartain folders on the nginx server. What is the best way to
do so?

Thanks for reading, I hope someone is able to help me.

Regards
Alfred

Hi,

But now the image requests don’t go through the proxy anymore,
resulting in a 404 error. Is there any way to inherit the proxy
settings from the parent folder/a certain location?
Of course I could just add the proxy settings to the “location
/images/” block, but the images folder is not the only one I want to
be cached, so there has to be an easier way to do this.

Settings in your / location will not be inherited by those in other
locations. The proxy_pass directive can only go in location or
location-if (i.e. an if (…) {…} inside a location), and can’t go at
the server level, so you’d have to define the proxy_pass setting in each
location it’s used.

http://wiki.nginx.org/NginxHttpProxyModule#proxy_pass

In other words: I want all requests going through the proxy, but keep
a cache of ceartain folders on the nginx server. What is the best way
to do so?

Probably by putting common settings (which might just be proxy_pass
http://backend.server.org:wink: in another file and including the file -
then you’d only have to change the address / port once.

If you have multiple locations you want to cache, you could have three
files:

proxy.conf : (with just the proxy info)

proxy_pass http://backend.server.org;

proxy_cache.conf : (with the proxy and the cache info)

include proxy.conf;
proxy_cache cache;
proxy_cache_valid 200 10m;

nginx.conf : (with the rest)


location / {
include proxy.conf;
}

location /images/ {
include proxy_cache.conf;
}

location /other-cache/ {
include proxy_cache.conf;
}

Hope that helps,

Marcus.

Hi,

Settings in your / location will not be inherited by those in other
locations. The proxy_pass directive can only go in location or
location-if (i.e. an if (…) {…} inside a location), and can’t go at
the server level, so you’d have to define the proxy_pass setting in each
location it’s used.

http://wiki.nginx.org/NginxHttpProxyModule#proxy_pass

Okay, that’s what I thought.

nginx.conf : (with the rest)
location /other-cache/ {
include proxy_cache.conf;
}

Hope that helps,

Marcus.

Thank you very much, Marcus, I didn’t think about “include” at all.
Although this way still seems kind of intricate to me, it is probably
the easiest way to get what I want. I will try it like that and hope it
works.

Regards,
Alfred

Hi,

Thank you very much, Marcus, I didn’t think about “include” at all.
You’re welcome.
Although this way still seems kind of intricate to me, it is probably
the easiest way to get what I want. I will try it like that and hope
it works.
Personally, I prefer to have things setup in such a way that if you want
to change something, you only do it in one place, even if that means
having a few files and using includes, because in my experience it’s
easier to maintain.

Marcus.

On Sat, Sep 05, 2009 at 12:00:39PM +0200, [email protected] wrote:

Okay, that’s what I thought.

files:

Although this way still seems kind of intricate to me, it is probably
the easiest way to get what I want. I will try it like that and hope it
works.

Another way is to put common settings on server/http level (and to
override
them in location’s):

server {
proxy_cache_valid 200 10m;

location / {
    proxy_pass  http://backend.server.org;
}

location /images/ {
    proxy_pass  http://backend.server.org;
    proxy_cache  cache;
    proxy_cache_valid  200 6h;
}

location /other-cache/ {
    proxy_pass  http://backend.server.org;
    proxy_cache  cache;
}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs