# Set generated PHP output to Memcached server by Nginx transparently

**URL:** <https://www.ruby-forum.com/t/set-generated-php-output-to-memcached-server-by-nginx-transparently/193428>\
**Category:** NGINX\
**Created:** [August 17, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/set-generated-php-output-to-memcached-server-by-nginx-transparently/193428 "2010-08-17T16:01:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkuang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@jkuang](https://www.ruby-forum.com/u/jkuang)\
**Post date:** [August 17, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/set-generated-php-output-to-memcached-server-by-nginx-transparently/193428/1 "2010-08-17T16:01:06Z")

</div>

Hi,

I have set up for NginxHttpMemcachedModule and it works great reading  
output that I have set to Memcached server through my OWN PHP code. But  
that means I have to modify my PHP code to make it happens and sometimes  
it is not easy if the code is third party. I really think it would be  
more beneficial if the NginxHttpMemcachedModule could set the generated  
PHP output into Memcached server. By doing so, I don’t have to modify  
PHP code to make it happen and it offers much better maintenance, using  
Nginx server as the Memcache (cluster) Proxy.

Basically I need to be able to read PHP output from Memcached server:

1. If there is PHP output in the Memcached server given $uri, use it
2. If not, use fallback to the fastcgi or PHP-FPM
3. The generated PHP output of step 2 is set to the Memcached server BY  
NGINX TRANSPARENTLY (no PHP code change but just Nginx configuration) to  
the Memcached server so it can be read next time on step 1.

I notice that there is another module call NginxHttpMemcModule. It seems  
that it could do the trick but I am not sure by looking at the  
configuration example in the wiki.

Could someone provide some insight on this issue?

Thanks!  
– Jin Kuang

Posted at Nginx Forum:

> **[Set generated PHP output to Memcached server by Nginx transparently](https://forum.nginx.org/read.php?2,120614,120614#msg-120614)**
>
> Hi, I have set up for NginxHttpMemcachedModule and it works great reading output that I have set to Memcached server through my OWN PHP code. But that means I have to modify my PHP code to make it happens and sometimes it is not easy if the code is...

---

<div class="post-metadata">

**Author:** ![jkuang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@jkuang](https://www.ruby-forum.com/u/jkuang)\
**Post date:** [August 17, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/set-generated-php-output-to-memcached-server-by-nginx-transparently/193428/2 "2010-08-17T19:48:56Z")

</div>

This is like the Holy Grail. See my similar request here  
[Please Help With Memcached Setup](http://forum.nginx.org/read.php?2,117833).

However, the answer I got was on the cryptic side with the only thing I  
could make out being it is not a good idea with proxy which I use as at  
yet so I gave up.

Posted at Nginx Forum:

> **[Re: Set generated PHP output to Memcached server by Nginx transparently](https://forum.nginx.org/read.php?2,120614,120755#msg-120755)**
>
> Hi, I have set up for NginxHttpMemcachedModule and it works great reading output that I have set to Memcached server through my OWN PHP code. But that means I have to modify my PHP code to make it happens and sometimes it is not easy if the code is...

---

<div class="post-metadata">

**Author:** ![jkuang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@jkuang](https://www.ruby-forum.com/u/jkuang)\
**Post date:** [August 18, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/set-generated-php-output-to-memcached-server-by-nginx-transparently/193428/3 "2010-08-18T18:03:12Z")

</div>

> However, the answer I got was on the cryptic side with the only thing I  
> could make out being it is not a good idea with proxy which I use as at  
> yet so I gave up.

Cryptic?

The module [GitHub - openresty/srcache-nginx-module: Transparent subrequest-based caching layout for arbitrary nginx locations. · GitHub](http://github.com/agentzh/srcache-nginx-module) has the  
ability to  
“cache” - store subrequest response in memcached backend (combined with  
memc  
or any other module).

rr

---

<div class="post-metadata">

**Author:** ![jkuang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@jkuang](https://www.ruby-forum.com/u/jkuang)\
**Post date:** [August 18, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/set-generated-php-output-to-memcached-server-by-nginx-transparently/193428/4 "2010-08-18T17:27:28Z")

</div>

Dayo, thank you for the response. It seem that is what I am looking  
for. I am going to try it out and see if it works.

Given the fact that this is still on the “bleeding edge” side of Nginx  
usage, I am thinking it might be better off if I will just use a  
“middleware” PHP to handle the update to the memcached server.

1. If there is PHP output in the Memcached server cluster given $uri,  
use it (with header stuff set)

2. If not, REWRITE the URL for  
[http://mysite.com/memcached\_update.php?location=the\_original\_url](http://mysite.com/memcached_update.php?location=the_original_url)

This memcached\_update.php will open up a output buffer, perform a url  
get for the content of the original url, store the buffered content in  
memcached server (unfortunately that means this PHP will need to perform  
memcached server look up and cluster management in a clustered memcached  
environment) and return the content

1. Next time the same location is requested, we should have the content  
available in the memcached server cluster.

This should not have impact on the original application as the  
“middleware” PHP is generic. But still it is a messy hack. It is not  
“pretty”

I really hope NginxHttpMemcachedModule or NginxHttpMemcModule should  
bundle this critical capability in the modules directly. It seems so  
obviouos this job belongs to these two modules.

Thanks  
– Jin Kuang

Posted at Nginx Forum:

> **[Re: Set generated PHP output to Memcached server by Nginx transparently](https://forum.nginx.org/read.php?2,120614,121015#msg-121015)**
>
> Hi, I have set up for NginxHttpMemcachedModule and it works great reading output that I have set to Memcached server through my OWN PHP code. But that means I have to modify my PHP code to make it happens and sometimes it is not easy if the code is...
