# Nginx and Memcache

**URL:** <https://www.ruby-forum.com/t/nginx-and-memcache/197917>\
**Category:** NGINX\
**Created:** [October 22, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917 "2010-10-22T15:40:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 22, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/1 "2010-10-22T15:40:55Z")

</div>

Hi,

I have recently installed Nginx as a front facing server to communicate  
with memcache. My problem is that the PHP code that writes into memcache  
serializes the contents. I cannot turn off serialization but need to  
figure out how to get the string content from memcache. The current  
output from Nginx is of type application/octet-stream. Is there  
something I missed?

Thank you.

Regards  
Anil

Posted at Nginx Forum:

> **[Nginx and Memcache](https://forum.nginx.org/read.php?2,143497,143497#msg-143497)**
>
> Hi, I have recently installed Nginx as a front facing server to communicate with memcache. My problem is that the PHP code that writes into memcache serializes the contents. I cannot turn off serialization but need to figure out how to get the string...

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 22, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/2 "2010-10-22T17:55:49Z")

</div>

Hello!

On Fri, Oct 22, 2010 at 09:39:03AM -0400, midas wrote:

> Hi,
> 
> I have recently installed Nginx as a front facing server to communicate  
> with memcache. My problem is that the PHP code that writes into memcache  
> serializes the contents. I cannot turn off serialization but need to  
> figure out how to get the string content from memcache. The current  
> output from Nginx is of type application/octet-stream. Is there  
> something I missed?

As memcached values doesn’t store/return any content type - nginx only  
able to detect it based on request uri extension (i.e. the same  
logic as for static files applies).

See details here:

> **[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

> **[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

E.g to make sure all replies from memcached are returned as  
text/plain do something like this:

```
location /memcached/ {
    types {}
    default_type text/plain;
    memcached_pass ...
}

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 22, 2010, 9:26pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/3 "2010-10-22T21:26:11Z")

</div>

Thanks Maxim

My problem is that the value coming from memcache is a php serialized  
string content (byte-stream). I was able to change the content send from  
nginx with your suggestion. But I still get errors since the response  
contains invalid characters.

BTW is there a way to cache response from memcache in Nginx so that the  
server does not have to keep going to memcache for data?

Regards  
Anil

Posted at Nginx Forum:

> **[Re: Nginx and Memcache](https://forum.nginx.org/read.php?2,143497,143619#msg-143619)**
>
> Hi, I have recently installed Nginx as a front facing server to communicate with memcache. My problem is that the PHP code that writes into memcache serializes the contents. I cannot turn off serialization but need to figure out how to get the string...

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 22, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/4 "2010-10-22T22:18:30Z")

</div>

Hello!

On Fri, Oct 22, 2010 at 03:24:56PM -0400, midas wrote:

> My problem is that the value coming from memcache is a php serialized  
> string content (byte-stream). I was able to change the content send from  
> nginx with your suggestion. But I still get errors since the response  
> contains invalid characters.

You should put non-serialized data into memcached.

> BTW is there a way to cache response from memcache in Nginx so that the  
> server does not have to keep going to memcache for data?

No. It’s presumed that memcached is fast enough to not require  
additional caching.

Maxim D.

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 22, 2010, 11:06pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/5 "2010-10-22T23:06:24Z")

</div>

Thanks Maxim,

This helped a lot. I was able to dig into the code and remove  
serialization.

Was thinking about file caching in Nginx. Is there a way to configure  
file caching? I apologize for all the questions. Kinda new to Nginx.  
I need the server to respond to a very high amount of traffic.

Regards  
Anil

Posted at Nginx Forum:

> **[Re: Nginx and Memcache](https://forum.nginx.org/read.php?2,143497,143644#msg-143644)**
>
> Hi, I have recently installed Nginx as a front facing server to communicate with memcache. My problem is that the PHP code that writes into memcache serializes the contents. I cannot turn off serialization but need to figure out how to get the string...

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 26, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/6 "2010-10-26T17:40:05Z")

</div>

Hi,

I finally have Nginx working with memcache and doing its job perfectly.  
I have a question regarding the memcache\_pass configuration. Is it  
possible to have a pool of memcache servers instead of one memcache  
server?

Regards  
Anil

Posted at Nginx Forum:

> **[Re: Nginx and Memcache](https://forum.nginx.org/read.php?2,143497,144691#msg-144691)**
>
> Hi, I have recently installed Nginx as a front facing server to communicate with memcache. My problem is that the PHP code that writes into memcache serializes the contents. I cannot turn off serialization but need to figure out how to get the string...

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 26, 2010, 7:16pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/7 "2010-10-26T19:16:52Z")

</div>

Hello!

On Tue, Oct 26, 2010 at 11:38:41AM -0400, midas wrote:

> Hi,
> 
> I finally have Nginx working with memcache and doing its job perfectly.  
> I have a question regarding the memcache\_pass configuration. Is it  
> possible to have a pool of memcache servers instead of one memcache  
> server?

You are free to use defined upstream[1] in memcached\_pass. By  
default nginx will use round-robin though. Blancing methods  
compatible with Cache::Memcached and other memcached client  
libraries are available as third party modules[2][3].

[1] [http://wiki.nginx.org/HttpUpstreamModule](http://wiki.nginx.org/HttpUpstreamModule)  
[2] [http://openhack.ru/nginx-patched/wiki/MemcachedHash](http://openhack.ru/nginx-patched/wiki/MemcachedHash)  
[3] [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpUpstreamConsistentHash)

Maxim D.

---

<div class="post-metadata">

**Author:** ![midas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@midas](https://www.ruby-forum.com/u/midas)\
**Post date:** [October 26, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/nginx-and-memcache/197917/8 "2010-10-26T19:35:41Z")

</div>

Awesome! I had read about the upstream module. I was not sure if it  
could be used along with memcache\_pass.  
Thanks Maxim.

On another topic altogether, is there an APC module for Nginx?

Regards  
Anil

Posted at Nginx Forum:

> **[Re: Nginx and Memcache](https://forum.nginx.org/read.php?2,143497,144725#msg-144725)**
>
> Hi, I have recently installed Nginx as a front facing server to communicate with memcache. My problem is that the PHP code that writes into memcache serializes the contents. I cannot turn off serialization but need to figure out how to get the string...
