Re: 请教关于ningx lua gzip压缩的问题

160 views
Skip to first unread message

Yichun Zhang (agentzh)

unread,
Jul 13, 2013, 3:34:45 PM7/13/13
to 曾魁, openresty
Hello!

2013/7/13 曾魁:
> 为了更加简单的说明我的问题,我写了一个简单 content_by_lua_file 代码如下:
> local zlib = require "zlib"
> local compress = zlib.deflate()
> local mystr = compress("hello", 'finish')
>
> ngx.header.content_type = "text/html"
> ngx.header.content_encoding = "gzip"
>
> ngx.say(mystr)
>
> 在浏览器进行访问的时候,是不能正常显示“hello”这个字符串的。
>

你的 Lua 代码存在几个问题:

1. zlib.deflate() 返回的压缩数据是 deflate 格式的,而不是 gzip 格式,所以你应当把
Content-Encoding 响应头置为 deflate 而不是 gzip. 关于二者的区别,可以参考
http://stackoverflow.com/questions/388595/why-use-deflate-instead-of-gzip-for-text-files-served-by-apache

2. ngx.say() 会自动在输出末尾追加换行符,所以你应当换用 ngx.print() 函数。细节见
http://wiki.nginx.org/HttpLuaModule#ngx.say

3. 你应当总是检查 Accept-Encoding 请求头中是否存在 deflate 一项,以确保 HTTP 客户端是支持此种压缩格式的。

同时抄送给 openresty 中文邮件列表:https://groups.google.com/group/openresty

建议你加入此列表并总是在那里讨论这样的问题(除非涉及安全问题)。谢谢合作!

Best regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages