在OpenResty中使用lua-zlib的方法
1、檢視 zlib在centos 中是否存在?
rpm -qa | grep zlib
顯示:
zlib-devel-1.2.3-29.el6.x86_64
zlib-1.2.3-29.el6.x86_64
表示已安裝,不用過多擔心 。
====================================================================
2、安裝cmake編譯器
yum install -y gcc gcc-c++ make automake
wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
tar -zxvf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./bootstrap
gmake
gmake install
檢查cmake安裝
cmake --version
顯示
cmake version 2.8.10.2
表示安裝成功
====================================================================
3、下載lua-zlib包,並解壓
unzip lua-zlib-master.zip
cd /usr/local/software/lua-zlib-master
cmake -DLUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1 -DLUA_LIBRARIES=/usr/local/openresty/luajit/lib -DUSE_LUAJIT=ON -DUSE_LUA=OFF
make
cp zlib.so /usr/local/openresty/lualib/zlib.so
====================================================================
4、在lua指令碼中呼叫
location /test { default_type text/html; content_by_lua ' local zlib = require "zlib" local encoding = ngx.req.get_headers()["Content-Encoding"] -- post引數在接收前首先要執行這個 ngx.req.read_body(); if encoding == "gzip" then local body = ngx.req.get_body_data() if body then local stream = zlib.inflate() local r=stream(body); ngx.req.set_body_data(r); end else ngx.say("輸入的內容未經過gzip壓縮。"); ngx.exit(ngx.HTTP_OK); end --輸出引數看看 local args = ngx.req.get_post_args() for key, val in pairs(args) do if type(val) == "table" then ngx.say(table.concat(val, ", ")) else ngx.say(val) end end '; }
====================================================================
5、用c#來模組提交gzip壓縮後的資料到伺服器
private void button3_Click(object sender, EventArgs e)
{
var url = "http://192.168.1.100/test";
var body = "body=黃海是我的名字!";
var ret=HttpUtil.PostHttpByGzip(url, body);
Console.WriteLine(ret);
}
/// <summary>
/// 功能:發起POST請求,可選擇是否使用在發起時的BODY GZIP壓縮
/// 作者:黃海
/// 時間:2015-01-02
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string PostHttpByGzip(string url, string body)
{
var req = WebRequest.Create(url);
req.Method = "POST"; // "post"
req.Timeout = 20000;
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("Content-Encoding", "gzip");
var reqStream = req.GetRequestStream();
var gz = new GZipStream(reqStream, CompressionMode.Compress);
var sw = new StreamWriter(gz, Encoding.UTF8);
sw.Write(body);
sw.Close();
gz.Close();
reqStream.Close();
var myResponse = req.GetResponse();
var sr = new StreamReader(myResponse.GetResponseStream());
var ret=sr.ReadToEnd();
sr.Close();
myResponse.Close();
return ret;
}
====================================================================
問題總結:
Makefile是linux下面的檔案,對於一個包含很多檔案的工程,如果直接編譯,那麼我們就需要使用一些命令將所有的檔案都包括進來。如果我們對其中的一些檔案稍做修改,那麼我們需要重新輸入這些命令。Makefile檔案就可以很好的解決這個問題,它將所需要的命令都包含在這個Makefile檔案中,然後簡單的make一下就完成了所有的步驟。