# Proxy cache for php site

**URL:** <https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857>\
**Category:** NGINX\
**Created:** [October 16, 2011, 1:41am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857 "2011-10-16T01:41:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 16, 2011, 1:41am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/1 "2011-10-16T01:41:07Z")

</div>

Hi,

I’m trying to implement this style of proxy\_cache config  
[http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n](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://pastie.org/2703119)

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

Grateful for any advice.

thanks,

Jon

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [October 16, 2011, 2:50am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/2 "2011-10-16T02:50:44Z")

</div>

On 16 Out 2011 00h40 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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://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.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

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

— appa

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [October 16, 2011, 3:17am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/3 "2011-10-16T03:17:17Z")

</div>

Hi Appa,

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

No doubt, any tips on cleaning up appreciated 🙂

> 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.
> 
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](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](http://pastie.org/2703119)

Thanks,

Jon

–  
jon bennett - [www.jben.net](http://www.jben.net) - [blog.jben.net](http://blog.jben.net)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [October 16, 2011, 2:25pm UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/4 "2011-10-16T14:25:48Z")

</div>

Hi Appa,

> Try this: [Example microcache configuration · GitHub](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

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [October 16, 2011, 4:54am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/5 "2011-10-16T04:54:37Z")

</div>

On 16 Out 2011 02h16 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](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](http://pastie.org/2703119)

Try this: [Example microcache configuration · GitHub](https://gist.github.com/1290446)

— appa

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 8, 2011, 5:57pm UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/6 "2011-11-08T17:57:37Z")

</div>

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](http://forum.nginx.org/read.php?2,216756,217993#msg-217993)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 8, 2011, 10:29pm UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/7 "2011-11-08T22:29:49Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [October 17, 2011, 1:02am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/8 "2011-10-17T01:02:21Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Bit of a weird one, will investigate further.
> 
> Thanks,
> 
> Jon

–  
jon bennett - [www.jben.net](http://www.jben.net) - [blog.jben.net](http://blog.jben.net)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 9, 2011, 12:03pm UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/9 "2011-11-09T12:03:40Z")

</div>

## Maxim D. Wrote:

> clients with the  
> work, notably  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](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](http://forum.nginx.org/read.php?2,216756,218066#msg-218066)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 8, 2011, 10:31pm UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/10 "2011-11-08T22:31:05Z")

</div>

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] [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpProxyModule#proxy_ignore_headers)  
[2] [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](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.

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 9, 2011, 2:19pm UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/11 "2011-11-09T14:19:21Z")

</div>

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](http://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](http://forum.nginx.org/read.php?2,216756,218084#msg-218084)

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 10, 2011, 8:50am UTC](https://www.ruby-forum.com/t/proxy-cache-for-php-site/212857/12 "2011-11-10T08:50:25Z")

</div>

> does not match “”, client: 10.214.99.16, server:  
> \*.site.com, request: “GET / HTTP/1.1”, host:  
> “[www.site.com](http://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](http://forum.nginx.org/read.php?2,216756,218143#msg-218143)
