# Memcache and proxy

**URL:** <https://www.ruby-forum.com/t/memcache-and-proxy/147615>\
**Category:** NGINX\
**Created:** [September 3, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/memcache-and-proxy/147615 "2008-09-03T17:25:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 3, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/memcache-and-proxy/147615/1 "2008-09-03T17:25:31Z")

</div>

I am using nginx as the front end for our website, proxying PHP requests  
off to  
another server.  
I want to make sure nginx catches 502 and 504 errors (incase the php  
backend is  
down) and redirect these errors to a static page.

So i have this working fine

#serve static pages with nginx  
location ~\* ^.+.(swf|xml|png|html|jpg|jpeg|gif|pdf|ico|css|js)$ {  
root /data/htdocs/;  
}

#proxy the rest to backend  
location / {  
index index.php ;  
error\_page 504 = /index.html;  
error\_page 502 = /index.html;  
proxy\_pass [http://10.100.250.100:88](http://10.100.250.100:88);  
}

If my backend is working, site works fine, if i shut down the php server  
i get  
the index.html page. Works like a charm.

However, I want to also use memcache, so that some php pages can get  
served from  
cache and not have to be sent to the backend

so i change this to my conf

location / {  
index index.php ;  
set $memcached\_key “guest:Action:$uri”;  
memcached\_pass 10.100.250.101:11220;  
default\_type text/html;  
error\_page 404 = /fallback;  
}

location = /fallback {  
proxy\_pass [http://10.100.250.100:88](http://10.100.250.100:88);  
error\_page 502 = /index.html;  
error\_page 504 = /index.html;  
}

When my backend server is up, this too works fine. I can store pages in  
cache  
and they get displayed and if the page isnt in cache, the backed will  
serve it  
up. However, if i shutdown my backend server, the 502 and 504 redirects  
no  
longer work. I see the 502 Bad Gateway nginx/0.6.32 error page. Nothing  
i do  
seems to fix this. As soon as i add the fallback location for memcache,  
i can no  
longer catch the 502 or 504 errors.

any idea how to fix this???

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 3, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/memcache-and-proxy/147615/2 "2008-09-03T17:58:57Z")

</div>

Hello!

On Wed, Sep 03, 2008 at 02:23:50PM +0000, Paul B wrote:

> root /data/htdocs/;
> 
> set $memcached\_key “guest:Action:$uri”;
> 
> When my backend server is up, this too works fine. I can store pages in cache  
> and they get displayed and if the page isnt in cache, the backed will serve it  
> up. However, if i shutdown my backend server, the 502 and 504 redirects no  
> longer work. I see the 502 Bad Gateway nginx/0.6.32 error page. Nothing i do  
> seems to fix this. As soon as i add the fallback location for memcache, i can no  
> longer catch the 502 or 504 errors.
> 
> any idea how to fix this???

[http://wiki.codemongers.com/NginxHttpCoreModule#recursive\_error\_pages](http://wiki.codemongers.com/NginxHttpCoreModule#recursive_error_pages)

Maxim D.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 5, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/memcache-and-proxy/147615/3 "2008-09-05T18:36:49Z")

</div>

Maxim D. \<mdounin@…\> writes:

> [http://wiki.codemongers.com/NginxHttpCoreModule#recursive\_error\_pages](http://wiki.codemongers.com/NginxHttpCoreModule#recursive_error_pages)
> 
> Maxim D.

Excellent, I knew it would have a simple answer.  
Hopefully there is an as simple fix to the next problem i have  
again concerning memcache and proxy

I have a url  
[http://my.domain.com/something/one](http://my.domain.com/something/one)

i don’t cache it, so it passes it to the proxy.

What the page should do is a simple redirect to

[http://my.domain.com/something/one/content](http://my.domain.com/something/one/content)

If i remove my memcache line, so that everything in location / goes to  
proxy\_pass [http://backend](http://backend);

it works  
If i go direct to the proxy port, it works  
However, using my memcache config, it passes it to my /fallback  
locations and it  
stops working. I get a blank page in my browser and it doesn’t redirect.  
I can  
check the logs of my backend, and I see exactly the same request either  
way, it  
is just that when i pass it to /fallback, the redirect does not seem to  
get  
passed back to nginx.
