# 環境準備
lua環境5.1 以上
安裝lua-cjson-2.1.0模塊 php
第一步:下載地址,http://www.kyne.com.au/~mark/software/lua-cjson.php
下載lua-cjson-2.1.0.tar.gz nginx
第二步:解壓cjsonc++
sudo tar zxvf lua-cjson-2.1.0.tar.gz
第三步: make
若是出現報錯
redis
find / -name lua.h
結果:json
/usr/local/openresty/lua/lua-5.3.4/src/lua.h
修改Makefiledom
第四步:
從新curl
make make install
第五步:打開lua終端引入cjson測試
ide
沒有報錯則成功安裝了。測試
安裝Openresty
yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib libxml2-devel ncurses-devel bison libtool-ltdl-devel libiconv libmcrypt mhash mcrypt pcre-devel openssl-devel freetype-devel libcurl-devel readline-devel curl ghostscript wget https://openresty.org/download/openresty-1.11.2.5.tar.gz wget https://www.openssl.org/source/openssl-1.0.2g.tar.gz 解壓 tar -zxvf openresty-1.11.2.5.tar.gz tar -zxvf openssl-1.0.2g.tar.gz cd /usr/local/openssl-1.0.2g 編譯安裝openssl模塊 ./config --prefix=/usr/local/openssl make && make install cd /usr/local/zlib-1.2.11 ./configure --prefix=/usr/local/zlib 編譯安裝openresty make && make install cd openresty-1.11.2.5 ./configure --prefix=/usr/local/openresty --with-luajit --with-http_ssl_module --user=root --group=root --with-http_realip_module --with-pcre --with-http_realip_module --with-zlib=/usr/local/zlib-1.2.8 gmake gmake install
使用yum安裝redis,須要下載阿里雲yum源
yum install redis -y
ui
初始化配置就不寫了,網上一大片一大片的
接下來纔是重點Lua腳本內容
local cjson = require "cjson" local redis = require "resty.redis" local pool_max_idle_time = 10001 local pool_size = 100 local red = redis:new() local ok, err = red:connect('192.168.10.174','6379') red:set_timeout(1000) if not ok then ngx.say("failed to connect: ", err) return end local count, err = red:get_reused_times() if 0 == count then local ok, err = red:auth("123456") if not ok then ngx.say("failed to auth", err) return end local ok, err = red:select("0") if not ok then ngx.say("failed to select db: ", err) return end elseif err then ngx.say("failed to get reused times:", err) return end local request_uri = ngx.var.request_uri function spliturl( s, c ) for item in string.gmatch( s, "(.-)"..c) do urls = item urls = string.gsub(urls, "^/*(.-)%s*$", "%1") if not urls then ngx.say("failed to get urls: ", err) end end end spliturl(request_uri , "?" ) --domain is ip:prot local domain, err = red:get(urls) if not domain then ngx.exit(404) --ngx.say("failed to get proxied domain: ", err) --return elseif domain == ngx.null then ngx.exit(404) end --獲取token local headers = ngx.req.get_headers() local token = headers["token"] --拼接完整的redis key local redis_key = urls..":"..token local ok, err = red:select("1") if not ok then ngx.say("failed to select db: ", err) return end local getKey = red:get(redis_key) --ngx.say(getKey) if getKey == ngx.null then ngx.exit(403) end --將獲取到的value賦值給target ngx.var.target = domain red:close()
nginx配置文件
location ~* ^/ { #set $request_uri; #set $args; proxy_send_timeout 75; proxy_read_timeout 75; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With'; add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS'; resolver 114.114.114.114; default_type 'text/plain'; charset 'utf-8'; access_by_lua_file /usr/local/openresty/lua/redis.lua; set $target ''; proxy_pass http://$target; #$arg_name; }
使用Postman測試
正確的結果
輸入的path,redis會去庫中查若是沒有該鍵值時和返回的頁面
token錯誤時返回的結果
但願能夠幫助到有須要到同窗,有問題能夠隨時聯繫我