Why does nginx + memcache corrupt my response body?
I'm caching some web pages in memcache. When I read the page directly from the cache, the page is well formed like this ...
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"-:-- 0
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
but when I use a browser or curl to read it from nginx (version 0.8.50), it looks like response headers are ending up in the body of the response like this ...
�{
" ETag"'"16bb9f51667d334aa4e7663ca28d308a""X-Runtime177"Content-Type"text/html; charset=utf-8"Content-Length"5428"Set-Cookie""Cache-Control"(private, max-age=0, must-revalidate"4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
My nginx config 开发者_Go百科is pretty simple ...
set $memcached_key $cookie__app_session$uri;
memcached_pass localhost:11211;
default_type text/html;
error_page 404 502 /fallback$uri;
Does anyone have an idea why the response is corrupt?
Do! Stupid developer problem!
There were two mistakes
(a) I was storing the response header and body in memcache, then adding headers in an nginx rule. Storing only the response body in memcache removed the bulk of the problems
(b) I was storing the response in Ruby's marshal format (the default setting in memcache-client) - reading the contents of memcache using a simple Ruby client was hiding the fact that the format was not directly usable by nginx.
Hope that helps someone sometime!
Chris
精彩评论