# Memcache and garbled text returned by Nginx

**URL:** <https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206>\
**Category:** NGINX\
**Created:** [February 15, 2011, 4:37pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206 "2011-02-15T16:37:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 15, 2011, 4:37pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/1 "2011-02-15T16:37:43Z")

</div>

I have a php script that writes the whole page output (using output  
buffering and PECL memcache module) to memcache using keys such as:  
$host$request\_uri

```auto
<?php

function memcache_ob($output) {
  $time_start = microtime();
  $cache_ttl = 300;

  $key = trim($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

  $memcache = new Memcache;
  $memcache->connect('127.0.0.1', 11211);

  if ($cached = $memcache->get($key))
    return str_replace('', "<!-- From memcache in ". round(microtime() -
$time_start, 4) . " (" . date('r') . ") -->\n", $cached);

  $output = str_replace('', "<!-- Generated on " . date('r') . " / key: 
"
. $key . " -->\n", $output);

  $memcache->set($key, $output, false, $cache_ttl);

  return $output;
}

ob_start('memcache_ob');

?>
```

In nginx, I have the following:

```auto
location ~ \.php$ {
  set $memcached_key $host$request_uri;
  memcached_pass 127.0.0.1:11211;

  default_type text/html;
  error_page 404 502 504 405 = @cache_miss;
}

location @cache_miss {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;

  include /etc/nginx/fastcgi_params;
}
```

The problem is that I’m getting garbled text returned by nginx, which  
looks like this:

```auto
x�å\{sŪ¶²’;�¹ße;�=5õ
��meŅ�įČ¦ta^�Ųź!+Øąōćė/�~žCżĖONķ�ę�Ż¼žā�ņļžõŃ¶�óóēW?æ��}2r°ńÆLó³;d^"ŲÉ+ÖłµwōÕg8īšWÓ$'�õ�C¦8ą�Xn¦rĪL�£ĖŽ��±×B8w)gy*�÷Ķ�Ą£Ü¾\ßīUG¢x�Ī�Ł�@5šgāĒ½b®c¦&h:1„ŖG�D�(Ź�ōūÉæC£w 
a0źĶūæN<ŖpU�š{�)¤®1�+�z ¢Č`ŹžĮeŠÄ^r�ąB
P%ŖDų�®z�M�»��;§;ģ�
�RÆ:Ķ#0Ł>X�ĻšÉÄ'ž�2ŽŁ¹l�Qō³ÜP�¦��Ŗūlę��z�ś/fRÄ`ŪBī3;�I
+�Č�|Įč�éLØ”£�ē®!rź�c0’~�a�N
Ģõd5°³®Äė
×l7×�$�Ća'r
```

Posted at Nginx Forum:

> **[Memcache and garbled text returned by Nginx](https://forum.nginx.org/read.php?2,175470,175470#msg-175470)**
>
> I have a php script that writes the whole page output (using output buffering and PECL memcache module) to memcache using keys such as: $host$request\_uri \[code\] \<?php function memcache\_ob($output) { $time\_start = microtime(); $cache\_ttl = 300;

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 4:40pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/2 "2011-02-15T16:40:14Z")

</div>

Forgot to mention that I’m using Nginx 0.8.54.

Posted at Nginx Forum:

> **[Re: Memcache and garbled text returned by Nginx](https://forum.nginx.org/read.php?2,175470,175473#msg-175473)**
>
> I have a php script that writes the whole page output (using output buffering and PECL memcache module) to memcache using keys such as: $host$request\_uri \[code\] \<?php function memcache\_ob($output) { $time\_start = microtime(); $cache\_ttl = 300;

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 5:27pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/3 "2011-02-15T17:27:49Z")

</div>

Hello!

On Tue, Feb 15, 2011 at 10:36:02AM -0500, kaspars wrote:

> I have a php script that writes the whole page output (using output  
> buffering and PECL memcache module) to memcache using keys such as:  
> $host$request\_uri

[…]

> The problem is that I’m getting garbled text returned by nginx, which  
> looks like this:
> 
> x�å{sŪ¶²’;�¹ße;�=5õ

Looks like your client stores compressed data in memcached. Check  
your client documentation to find out how to switch it off.

Maxim D.

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 7:53pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/4 "2011-02-15T19:53:23Z")

</div>

On 2/15/11 1:38 PM, kaspars wrote:

> > Looks like your client stores compressed data in memcached. Check  
> > your client documentation to find out how to switch it off.
> 
> I already did that – I’m sure it does not compress data.
> 
> I just noticed that the problem occurs only for pages with a lot of HTML  
> (6KB), while smaller pages (3KB) are rendered fine. This is nowhere  
> close to the 1MB limit of memcache. For this page  
> [http://konstruktors.com/](http://konstruktors.com/) everything is OK, while this  
> [Blog about WordPress, Electronics and Software Products — Kaspars Dambis](http://konstruktors.com/blog/) returns the garbled string.

This page renders normally in current Mac versions of Firefox, Opera,  
and Safari for me.

> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)  
> –  
> Jim O.

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 7:53pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/5 "2011-02-15T19:53:23Z")

</div>

On 15.02.2011 19:38, kaspars wrote:

> I’ll try a different PHP memcache library, but I really doubt that it  
> will have any effect.

Hello,

one year ago i had a similar issue, i switched back to an older php pecl  
memcache extension and  
it solved my problem.

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 7:40pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/6 "2011-02-15T19:40:50Z")

</div>

> Looks like your client stores compressed data in memcached. Check  
> your client documentation to find out how to switch it off.

I already did that – I’m sure it does not compress data.

I just noticed that the problem occurs only for pages with a lot of HTML  
(6KB), while smaller pages (3KB) are rendered fine. This is nowhere  
close to the 1MB limit of memcache. For this page  
[http://konstruktors.com/](http://konstruktors.com/) everything is OK, while this  
[Blog about WordPress, Electronics and Software Products — Kaspars Dambis](http://konstruktors.com/blog/) returns the garbled string.

I’ll try a different PHP memcache library, but I really doubt that it  
will have any effect.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,175470,175541#msg-175541](http://forum.nginx.org/read.php?2,175470,175541#msg-175541)

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 8:06pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/7 "2011-02-15T20:06:34Z")

</div>

## Jim O. Wrote:

> > [http://konstruktors.com/](http://konstruktors.com/) everything is OK, while  
> > this  
> > [Blog about WordPress, Electronics and Software Products — Kaspars Dambis](http://konstruktors.com/blog/) returns the  
> > garbled string.
> 
> This page renders normally in current Mac versions  
> of Firefox, Opera,  
> and Safari for me.

No problem viewing the page either here as well.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,175470,175550#msg-175550](http://forum.nginx.org/read.php?2,175470,175550#msg-175550)

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 15, 2011, 10:08pm UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/8 "2011-02-15T22:08:55Z")

</div>

> one year ago i had a similar issue, i switched back to an older php  
> pecl  
> memcache extension and  
> it solved my problem.

That’s interesting, Alexander. I’ll give it a try.

And thanks to Jim and Dayo for reporting on the output of the page.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,175470,175575#msg-175575](http://forum.nginx.org/read.php?2,175470,175575#msg-175575)

---

<div class="post-metadata">

**Author:** ![kaspars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@kaspars](https://www.ruby-forum.com/u/kaspars)\
**Post date:** [February 16, 2011, 11:28am UTC](https://www.ruby-forum.com/t/memcache-and-garbled-text-returned-by-nginx/203206/9 "2011-02-16T11:28:41Z")

</div>

The problem was indeed caused by the php5-memcache PECL package I had  
installed from dotdeb (I guess php5-memcache\_5.3.5-0.dotdeb.0\_i386.deb).  
So I compiled a fresh one (from [http://pecl.php.net/package/memcache](http://pecl.php.net/package/memcache))  
using the official PECL installer and everything is perfect now.

Thanks to everyone for helping me figure this out.

Kaspars

Posted at Nginx Forum:

> **[Re: Memcache and garbled text returned by Nginx](https://forum.nginx.org/read.php?2,175470,175701#msg-175701)**
>
> I have a php script that writes the whole page output (using output buffering and PECL memcache module) to memcache using keys such as: $host$request\_uri \[code\] \<?php function memcache\_ob($output) { $time\_start = microtime(); $cache\_ttl = 300;
