Memcache and garbled text returned by Nginx

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

<?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:

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:

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:
http://forum.nginx.org/read.php?2,175470,175470#msg-175470

Forgot to mention that I’m using Nginx 0.8.54.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,175470,175473#msg-175473

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.

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/ everything is OK, while this
http://konstruktors.com/blog/ returns the garbled string.

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

http://nginx.org/mailman/listinfo/nginx

Jim O.

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.

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/ everything is OK, while this
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

Jim O. Wrote:

http://konstruktors.com/ everything is OK, while
this
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

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

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)
using the official PECL installer and everything is perfect now.

Thanks to everyone for helping me figure this out.

Kaspars

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,175470,175701#msg-175701

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs