【发布时间】:2015-05-20 21:17:42
【问题描述】:
我有一个简单的 web.py 代码,如下所示,在 apache 中使用 mod_wsgi 部署。
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
content = 'hello'
web.header('Content-length', len(content))
return content
app = web.application(urls, globals())
application = app.wsgifunc()
这个网站运行良好,除了一个小问题。当 mod_deflate 开启时,响应被分块,即使响应体很小。
响应头
HTTP/1.1 200 OK
Date: Wed, 20 May 2015 20:14:12 GMT
Server: Apache/2.4.7 (Ubuntu)
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
当 mod_deflate 关闭时,Content-Length 标头返回。
HTTP/1.1 200 OK
Date: Wed, 20 May 2015 20:30:09 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 5
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
我四处搜索,有人说减少 DeflateBufferSize 会有所帮助,但是这个响应的大小只有 5,远离它的默认值:8096,所以我认为它不会干扰这个问题。
有人说 apache 发送分块响应是因为它在开始向客户端发送响应之前不知道响应的大小,但是在我的代码中,我确实设置了 Content-Length。
我也尝试过Flask 和 Apache/2.2.15 (CentOS),结果相同。
启用 deflate 模块时如何设置内容长度?而且我不喜欢在 python 中压缩内容。
【问题讨论】: