Reverse Proxy with caching

Igor Sysoev is at rambler-co.ru
Thu Oct 15 18:22:16 MSD 2009


On Thu, Oct 15, 2009 at 03:17:20PM +0200, Smrchy wrote:

> Hi,
> 
> i setup nginx and it's working great for us for static files. I'm wondering
> if nginx would be suited for the following scenario:
> 
> We have an application server that has a lot of .php files and even more
> static files (.gif, .jpg etc).
> 
> Can i put nginx in front of if like a proxy but have nginx cache the static
> files (everything except the .php stuff). So that only .php requests reach
> the server and the static files will be cached on the nginx machine once the
> get retrieved from the upstream server.

First, you may separate static files from PHP:

server {

    location ~ \.(gif|jpg|png)$ {
        root                 /data/www;
    }

    location ~ \.php$ {
        proxy_pass           http://backend;
    }

However, you have to retrieve the files by yourself.

Second, you may use mirror on demand:

server {

    location ~ \.(gif|jpg|png)$ {
        root                 /data/www;
        error_page           404 = @fetch;
    }

    location @fetch {
        internal;

        proxy_pass           http://backend;
        proxy_store          on;
        proxy_store_access   user:rw  group:rw  all:r;
        proxy_temp_path      /data/temp;

        root                 /data/www;
    }

    location ~ \.php$ {
        proxy_pass           http://backend;
    }

And finally, you may use proxy cache:

proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m
                                     inactive=24h  max_size=1g;
server {

    location ~ \.(gif|jpg|png)$ {
        proxy_pass           http://backend;
        proyx_cache          STATIC;
        proyx_cache_valid    200  1d;
        proxy_cache_use_stale  error timeout invalid_header updating
                               http_500 http_502 http_503 http_504;
    }

    location ~ \.php$ {
        proxy_pass           http://backend;
    }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list