@dooy
2016-11-25T07:41:57.000000Z
字数 2497
阅读 180
Nginx
安装
软件 | 下载地址 |
---|---|
nginx | http://nginx.org/en/download.html |
luajit 2.0 | http://luajit.org/download.html |
ngx_devel_kit | https://github.com/simpl/ngx_devel_kit/tags |
nginx_lua_module | https://github.com/openresty/lua-nginx-module/tags |
cjson(luajit cjson) | http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz |
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make && make install
make前修改配置文件
vi Makefile
LUA_INCLUDE_DIR = $(PREFIX)/include/luajit-2.0
wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
tar -zvxf lua-cjson-2.1.0.tar.gz
cd lua-cjson-2.1.0
make
make install
解压 nginx ngx_devel_kit nginx_lua_module http-concat
cd到nginx目录编译
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz -O v0.2.19.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.2.tar.gz -O v0.10.2.tar.gz
wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip
tar xvf v0.2.19.tar.gz
tar xvf v0.10.2.tar.gz
unzip nginx-http-concat-master.zip
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx/ --add-module=../ngx_devel_kit-0.2.19 --add-module=../lua-nginx-module-0.10.2 --add-module=../nginx-http-concat-master --with-http_ssl_module --with-http_stub_status_module
make && make install
wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip
unzip nginx-http-concat-master.zip
cd nginx-dir
./configure --prefix=/usr/local/nginx/ --add-module=../nginx-http-concat-master --with-http_ssl_module
make && make install
在 /usr/local/nginx/conf/nginx.conf 中加入下面的代码
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
重启nginx
执行 http://ip/hello
如果有 出现 hello,lua 说明就安装成功了
vim /usr/local/nginx/lua/checklogin.lua
function checkIslogin( str )
str = ngx.unescape_uri(str);
local gckey="111";
local cjson = require "cjson"
local sjon = cjson.new().decode( str );
local nowkey= ngx.md5( string.format("%s%s%s", sjon.a, gckey, sjon.b));
-- ngx.log(ngx.ERR, "do login ", nowkey )
return nowkey==sjon.k
end
local isLogin= false
pcall( function(istr) isLogin= checkIslogin(istr) end , ngx.var.cookie_userinfo )
if isLogin== false then
--ngx.log(ngx.ERR, "do login ", ngx.var.cookie_userinfo )
return ngx.exit(403)
end
return
在 http server location 配置
access_by_lua_file /usr/local/nginx/lua/checklogin.lua;
比如:
location /wangtocheck {
default_type 'text/html';
access_by_lua_file /usr/local/nginx/lua/checklogin.lua;
}