[关闭]
@dooy 2016-11-25T07:41:57.000000Z 字数 2497 阅读 180

Nginx Lua cookie认证

Nginx 安装


1.安装

1.1 准备需要的软件

软件 下载地址
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

1.2 安装 luajit

  1. wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
  2. tar xf LuaJIT-2.0.4.tar.gz
  3. cd LuaJIT-2.0.4
  4. make && make install

1.3 安装 cjson

make前修改配置文件
vi Makefile
LUA_INCLUDE_DIR = $(PREFIX)/include/luajit-2.0

  1. wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
  2. tar -zvxf lua-cjson-2.1.0.tar.gz
  3. cd lua-cjson-2.1.0
  4. make
  5. make install

1.4 安装 nginx

解压 nginx ngx_devel_kit nginx_lua_module http-concat
cd到nginx目录编译

  1. wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz -O v0.2.19.tar.gz
  2. wget https://github.com/openresty/lua-nginx-module/archive/v0.10.2.tar.gz -O v0.10.2.tar.gz
  3. wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip
  4. tar xvf v0.2.19.tar.gz
  5. tar xvf v0.10.2.tar.gz
  6. unzip nginx-http-concat-master.zip
  7. cd nginx-1.8.0
  8. ./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
  9. make && make install

1.4.1 安装 nginx 只支持http-concat

  1. wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip
  2. unzip nginx-http-concat-master.zip
  3. cd nginx-dir
  4. ./configure --prefix=/usr/local/nginx/ --add-module=../nginx-http-concat-master --with-http_ssl_module
  5. make && make install

1.5 配置测试

在 /usr/local/nginx/conf/nginx.conf 中加入下面的代码

  1. location /hello {
  2. default_type 'text/plain';
  3. content_by_lua 'ngx.say("hello, lua")';
  4. }

重启nginx
执行 http://ip/hello
如果有 出现 hello,lua 说明就安装成功了

2.lua 编程

2.1 lua编程

vim /usr/local/nginx/lua/checklogin.lua

  1. function checkIslogin( str )
  2. str = ngx.unescape_uri(str);
  3. local gckey="111";
  4. local cjson = require "cjson"
  5. local sjon = cjson.new().decode( str );
  6. local nowkey= ngx.md5( string.format("%s%s%s", sjon.a, gckey, sjon.b));
  7. -- ngx.log(ngx.ERR, "do login ", nowkey )
  8. return nowkey==sjon.k
  9. end
  10. local isLogin= false
  11. pcall( function(istr) isLogin= checkIslogin(istr) end , ngx.var.cookie_userinfo )
  12. if isLogin== false then
  13. --ngx.log(ngx.ERR, "do login ", ngx.var.cookie_userinfo )
  14. return ngx.exit(403)
  15. end
  16. return

2.2 nginx配置

在 http server location 配置
access_by_lua_file /usr/local/nginx/lua/checklogin.lua;
比如:

  1. location /wangtocheck {
  2. default_type 'text/html';
  3. access_by_lua_file /usr/local/nginx/lua/checklogin.lua;
  4. }

3. 参考

  1. 参考《如何安装nginx_lua_module模块》
  2. 参考《Nginx+Lua开发入门》
  3. Nginx API For Lua
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注