Proxy cache for php site

Hi,

I’m trying to implement this style of proxy_cache config
http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n as a
test locally, to stick in front of php sites.

This is what I have: http://pastie.org/2703119

http://local-nocache.domain.co.uk works as expected, but
http://local.domain.co.uk gives me a 502.

Grateful for any advice.

thanks,

Jon

On 16 Out 2011 00h40 WEST, [email protected] wrote:

Hi,

I’m trying to implement this style of proxy_cache config
http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n as a
test locally, to stick in front of php sites.

This is what I have: http://pastie.org/2703119

Your config has a lot of cruft, meaning a lot of bad Yoda stuff.

But the main issue is that you’re proxying to fastcgi not to another
HTTP server, hence the cache to be used is fastcgi cache, not proxy
cache.

http://wiki.nginx.org/HttpFcgiModule#fastcgi_cache

Which apart from a few options has basically the same directives with
proxy replaced by fastcgi.

— appa

Hi Appa,

Your config has a lot of cruft, meaning a lot of bad Yoda stuff.

No doubt, any tips on cleaning up appreciated :slight_smile:

But the main issue is that you’re proxying to fastcgi not to another
HTTP server, hence the cache to be used is fastcgi cache, not proxy
cache.

http://wiki.nginx.org/HttpFcgiModule#fastcgi_cache

Which apart from a few options has basically the same directives with
proxy replaced by fastcgi.

Ok, from scanning the docs, this seems about right, but nothing is
being written to the cache dir, this is where I’ve got to:
http://pastie.org/2703119

Thanks,

Jon


jon bennett - www.jben.net - blog.jben.net

Hi Appa,

Try this: https://gist.github.com/1290446

I’ve implemented what you suggested, and although got no errors, it
doesn’t look like it’s caching, no files appearing in the cache dir
(which I’ve 777 just to be sure!) and not getting a high rps either.

I’m tailing my nginx error log with nginx compiled with debug, and
there’s nothing in there about fcgi cache.

Bit of a weird one, will investigate further.

Thanks,

Jon

On 16 Out 2011 02h16 WEST, [email protected] wrote:

http://wiki.nginx.org/HttpFcgiModule#fastcgi_cache

Which apart from a few options has basically the same directives
with proxy replaced by fastcgi.

Ok, from scanning the docs, this seems about right, but nothing is
being written to the cache dir, this is where I’ve got to:
http://pastie.org/2703119

Try this: https://gist.github.com/1290446

— appa

I’m sorry, but I don’t understand the effect of cookies on proxy
caching. I’m trying to implement the same configuration as the one
referred to, and I’m also having trouble in that it doesn’t seem to be
caching. I’m also using httperf. For every request the backend does
return a different cookie. Is this what is preventing the caching from
happening?

In the debug log, I found some entries that refer to the cache. They are
below:

2011/11/08 20:51:45 [debug] 21434#0: *1 http cache key:
“httpwww.site.comGET/”
2011/11/08 20:51:45 [debug] 21434#0: *1 http script var: “”
2011/11/08 20:51:45 [debug] 21434#0: *1 add cleanup: 091D56A4
2011/11/08 20:51:45 [debug] 21434#0: shmtx lock
2011/11/08 20:51:45 [debug] 21434#0: slab alloc: 76 slot: 4
2011/11/08 20:51:45 [debug] 21434#0: slab alloc: B6F6B000
2011/11/08 20:51:45 [debug] 21434#0: shmtx unlock
2011/11/08 20:51:45 [debug] 21434#0: *1 http file cache exists: -5 e:0
2011/11/08 20:51:45 [debug] 21434#0: *1 cache file:
“/var/cache/nginx/5/86/629d786ceca8ff787779a3e4ccdc8865”
2011/11/08 20:51:45 [debug] 21434#0: *1 add cleanup: 091D56E8
2011/11/08 20:51:45 [debug] 21434#0: *1 http upstream cache: -5

What do they mean? Thank you.

Jon B. Wrote:

Hi Appa,

got it sorted thanks, had cookies being written
all the time and that
was stopping it. Now in production, many thanks!

Jon

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,216756,217993#msg-217993

On Tue, Nov 08, 2011 at 11:56:53AM -0500, lpugoy wrote:

Hi there,

I’m sorry, but I don’t understand the effect of cookies on proxy
caching.

Caching involves fetching something once from the backend, and then
sending the same response to many requests.

A cookie is something that is set differently per request.

If you have one, you don’t have the other.

By default.

I’m trying to implement the same configuration as the one
referred to, and I’m also having trouble in that it doesn’t seem to be
caching. I’m also using httperf. For every request the backend does
return a different cookie. Is this what is preventing the caching from
happening?

Probably.

There are (rfc) rules on caching and cacheability of a http response –
but be aware that not all of them apply to a reverse proxy like nginx.

Look at the http headers sent from your backend. If they indicate that
the
response is not cacheable, nginx won’t cache it – unless you configure
it to. Setting a cookie is one way to make a response non-cacheable.

If you don’t want the cookie set, don’t set it.

Out-of-the-box, nginx obeys the rules. Get your backend to obey the
rules too, and it will all work fine.

If you won’t do that, then you can (probably) configure nginx to break
the
rules in the specific way that you want it to when proxying your
backend.

For example, if the backend sets a cookie and you want nginx to cache
that, do you want it to set the same cookie for all requests; or to set
no cookie for all requests; or something else?

You’re likely much better off fixing the backend.

Good luck,

f

Francis D. [email protected]

Hi Appa,

got it sorted thanks, had cookies being written all the time and that
was stopping it. Now in production, many thanks!

Jon

On 16 October 2011 13:25, Jon B. [email protected] wrote:

Bit of a weird one, will investigate further.

Thanks,

Jon


jon bennett - www.jben.net - blog.jben.net

Maxim D. Wrote:

clients with the
work, notably
http://wiki.nginx.org/HttpProxyModule#proxy_hide_h
eader

Hello.

Thank you for the explanation. I now understand the problem and it is
now working.

I want to add a condition where if a cookie with the name sess_id is
included in the request, then the cache would be bypassed. I tried the
following

if ($http_cookie ~* “sess_id”) {
set $no_cache “1”;
}

proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;

but it doesn’t seem to work. What is the correct way to do this? Thank
you very much.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,216756,218066#msg-218066

Hello!

On Tue, Nov 08, 2011 at 11:56:53AM -0500, lpugoy wrote:

I’m sorry, but I don’t understand the effect of cookies on proxy
caching. I’m trying to implement the same configuration as the one
referred to, and I’m also having trouble in that it doesn’t seem to be
caching. I’m also using httperf. For every request the backend does
return a different cookie. Is this what is preventing the caching from
happening?

Yes, nginx won’t cache responses with cookies unless you
specifically ask it to via proxy_ignore_headers directive[1].

Please also note that if you do so, you may want to also instruct
nginx to hide returned cookies from clients (via proxy_hide_header
directive[2]), or you’ll end up with multiple clients with the
same cookie.

Example configuration should look like:

proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;

Depending on the actual headers your backend returns you may also
need to ignore other headers as well for cache to work, notably
Expires and Cache-Control.

Alternatively, you may want to instruct your backend to not return
headers which prevent caching.

[1] http://wiki.nginx.org/HttpProxyModule#proxy_ignore_headers
[2] http://wiki.nginx.org/HttpProxyModule#proxy_hide_header

2011/11/08 20:51:45 [debug] 21434#0: shmtx unlock
2011/11/08 20:51:45 [debug] 21434#0: *1 http file cache exists: -5 e:0
2011/11/08 20:51:45 [debug] 21434#0: *1 cache file:
“/var/cache/nginx/5/86/629d786ceca8ff787779a3e4ccdc8865”
2011/11/08 20:51:45 [debug] 21434#0: *1 add cleanup: 091D56E8
2011/11/08 20:51:45 [debug] 21434#0: *1 http upstream cache: -5

What do they mean? Thank you.

This debug lines correspond to lookup of the request in cache, and
they show that no cached response exists.

Maxim D.

lpugoy Wrote:

if ($http_cookie ~* “sess_id”) {
set $no_cache “1”;
}

proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;

but it doesn’t seem to work. What is the correct
way to do this? Thank you very much.

An update. I don’t think that the if statement is being entered. There
are these messages in the debug log

2011/11/09 21:14:03 [debug] 6397#0: *3 http script var
2011/11/09 21:14:03 [debug] 6397#0: *3 http script regex: “sess_id”
2011/11/09 21:14:03 [notice] 6397#0: *3 “sess_id” does not match “”,
client: 10.214.99.16, server: *.site.com, request: “GET / HTTP/1.1”,
host: “www.site.com
2011/11/09 21:14:03 [debug] 6397#0: *3 http script if
2011/11/09 21:14:03 [debug] 6397#0: *3 http script if: false

Is there another way I can test for existence of cookies?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,216756,218084#msg-218084

does not match “”, client: 10.214.99.16, server:
*.site.com, request: “GET / HTTP/1.1”, host:
www.site.com
2011/11/09 21:14:03 [debug] 6397#0: *3 http script
if
2011/11/09 21:14:03 [debug] 6397#0: *3 http script
if: false

Is there another way I can test for existence of
cookies?

The problem is apparently with my method of passing cookies. curl -b
does not work. curl -b sess_id=… does. It’s now working
again.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,216756,218143#msg-218143

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