---
title: "lighttpd的modcache 不缓存的解决方法"
author: "iammonster"
date: "2009-09-09T16:20:24Z"
tags: []
slug: "lighttpd-modcache"
draft: false
lastmod: "2009-09-09T16:20:24Z"
id: 465607
url: "https://jiajun.iteye.com/blog/465607"
---

在使用modcache的过程中，遇到最多的问题是cache.refresh-pattern指明要缓存的URL没有缓存下来。

**页面内容是否缓存由且仅由后端服务器返回的HTTP头决定，和HTTP内容无关。**

调试工具：UNIX下用curl，Windows下用httpwatch，也可设置cache.debug = "enable"

一： 不缓存静态内容  
1\) 后端服务器返回压缩内容，相关的HTTP头是：  
Vary: Accept-Encoding  
Content-Encoding: gzip

> 解决方案：关闭后端服务器的gzip功能，启用前端lighttpd的mod\_compress 模块和 mod\_cache的memory\_compress选项

2\) 后端服务器返回的内容没有Content-Length和Transfer-Encoding: chunked  
此种情况下modcache不缓存的原因是不知道HTTP内容长度，就不能保证缓存的正确性，因此不缓存。

> 出现这种情况可能是URL被重写到一个php或jsp等程序，需要修改代码才可解决此问题

二：不缓存动态内容  
1\) 后端服务器返回Set-Cookie，相关的HTTP头是：  
Set-Cookie: cdb\_sid=xxxxx; expires=Sat, 29-Aug-09 03:57:12 GMT; path=/

> Set-Cookie 被缓存就会导致用户信息泄露，是绝对不能缓存的，是正常且正确的

2\) 后端服务器返回的头指定内容不要被缓存，相关的HTTP头是：  
Expires: -1  
Cache-Control: private, max-age=0  
Pragma: no-cache

> 这也是正常且正确的