Reverse Proxy with caching

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.

Thx for your help

Patrick

try ncache module, the cache module from Sina.COM

2009/10/15 Smrchy [email protected]:

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

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;
}

Hey,

On 15 okt 2009, at 16.22, Igor S. wrote:

static files (.gif, .jpg etc).

However, you have to retrieve the files by yourself.
location @fetch {
location ~ .php$ {
proxy_pass http://backend;
}

And finally, you may use proxy cache:

You could also add the cache purge patch to your stack if you need to
invalidate cached content: FRiCKLE Labs / nginx / cache_purge
(applies to 0.8.x)

Hi,

I’ve been trying to fix the cache for a nginx configured like reverse
proxy; I’m using these configuration parameter in my nginx.conf;
however, when I try to login to my back-end server, the server shows me
the next message in browser: “Your session has expired.”

May anyone help me???

proxy_buffering on;
proxy_cache_min_uses 3;
proxy_cache_path /usr/local/nginx/proxy_temp/ levels=1:2
keys_zone=cache:10m inactive=10m max_size=1000M;
proxy_cache_valid any 10m;
proxy_ignore_client_abort off;
proxy_intercept_errors on;
proxy_next_upstream error timeout invalid_header;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_connect_timeout 75;

Thanks a lot
proxy_send_timeout 90;
proxy_read_timeout 180;

Posted at Nginx Forum:

I am also trying to get the caching to work. Whenever I use the
proxy_cache_path or proxy_cache directive I get this error when starting
nginx and it fails to start- unknown directive “proxy_cache” - unknown
directive “proxy_cache_path”. Anyone know what I’m doing wrong? I am
using Ubuntu 9.04 and installed NGINX from the repos.

Posted at Nginx Forum:

Thx to all of you. I love how active and productive this group is. Gonna
give it a try with Igors hints and will check out the ncache on the next
weekend.

All the best

Patrick

Hi,

Just curious.
Is there any advantage to cache static files served directly from nginx?

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

Thanks

Posted at Nginx Forum:

Hello!

On Mon, Oct 19, 2009 at 06:06:25PM -0400, avery wrote:

I am also trying to get the caching to work. Whenever I use the proxy_cache_path or proxy_cache directive I get this error when starting nginx and it fails to start- unknown directive “proxy_cache” - unknown directive “proxy_cache_path”. Anyone know what I’m doing wrong? I am using Ubuntu 9.04 and installed NGINX from the repos.

Cache support first appeared in nginx 0.7.45. Ubuntu 9.04 seems
to have 0.6.35 in the package list.

Maxim D.

I haven’t personally tested it, but I doubt it if they are originating
from the same server. Maybe someone else will have actually run the
numbers. I am catching files that are being served using a reverse
proxy, which I’ve found helps out a fair amount.

Posted at Nginx Forum:

infestdead Wrote:

: “proxy_pass” may not have URI part in location
Ivo

Hm I found out what the problem was,
You cant have proxy_pass http://ip:port if you have a regexp in the
location directive, if you do - you need to define upstream first :
upstream php_server {
server http://ip:port/;
}
and then
proxy_pass http://php_server;

That did it for me.
Cheers,
Ivo

Posted at Nginx Forum:

Igor S. Wrote:

We have an application server that has a lot of

}
    error_page           404 = @fetch;

    root                 /data/www;
}

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

When I try using proxy_pass within location ~ something I get :
: “proxy_pass” may not have URI part in location given by regular
expression, or inside named location, or inside the “if” statement, or
inside the “limit_except” block in /etc/nginx/nginx.conf:52

the line is proxy_pass http://127.0.0.1:8080/;

Do you have an idea what the problem might be?

Thanks,
Ivo

    proyx_cache          STATIC;


Igor S.
Igor Sysoev

Posted at Nginx Forum:

Maxim D. wrote:

Hello!

On Mon, Oct 19, 2009 at 06:06:25PM -0400, avery wrote:

I am also trying to get the caching to work. Whenever I use the proxy_cache_path or proxy_cache directive I get this error when starting nginx and it fails to start- unknown directive “proxy_cache” - unknown directive “proxy_cache_path”. Anyone know what I’m doing wrong? I am using Ubuntu 9.04 and installed NGINX from the repos.

Cache support first appeared in nginx 0.7.45. Ubuntu 9.04 seems
to have 0.6.35 in the package list.

Maxim D.

Hi, I have a similar error with version 0.8.36…

[emerg]: unknown directive “proxy_cache_path” in
/usr/local/nginx/nginx.conf:33

The line is the following:

proxy_cache_path /var/lib/nginx/cache levels=1:2
keys_zone=staticfilecache:180m max_size=500m;

what’s wrong? thank you

(nginx -v --> nginx version: nginx/0.8.36)

On Fri, Oct 30, 2009 at 06:59:32AM -0400, infestdead wrote:

scenario:
on the nginx machine once the
location ~ .php$ {
location ~ .(gif|jpg|png)$ {
all:r;
When I try using proxy_pass within location ~ something I get :
: “proxy_pass” may not have URI part in location given by regular expression, or inside named location, or inside the “if” statement, or inside the “limit_except” block in /etc/nginx/nginx.conf:52

the line is proxy_pass http://127.0.0.1:8080/;

Do you have an idea what the problem might be?

  • proxy_pass http://127.0.0.1:8080/;
    
  • proxy_pass http://127.0.0.1:8080;
    

Rafa P. wrote:

Hi, I have a similar error with version 0.8.36…

[emerg]: unknown directive “proxy_cache_path” in
/usr/local/nginx/nginx.conf:33

The line is the following:

proxy_cache_path /var/lib/nginx/cache levels=1:2
keys_zone=staticfilecache:180m max_size=500m;

what’s wrong? thank you

(nginx -v --> nginx version: nginx/0.8.36)

Solved: the problem only appears if all the configuration is in the
nginx.conf. Using “include files” works ok. My mistate, I thought both
solution are equivalent.