OpenResty 基於 NGINX 和 LuaJIT 的 Web 平台,集成Lua環境。
客戶端訪問圖片時,攜帶Token,Token為是session名稱。
openresty(NGINX)接收到請求后,匹配location。匹配上后,觸發lua腳本。
lua腳本獲取URI的token值,也就是session名稱。
獲取到session名稱后,lua腳本連接Redis后,將session名稱到Redis匹配。
如果Redis有session名稱的key。即返回成功。
否則返回500頁面到客戶端。
完成token驗證。
獲取安裝包。
[root@localhost ~]# wget https://openresty.org/download/openresty-1.19.3.1.tar.gz [root@localhost ~]# tar zxf openresty-1.19.3.1.tar.gz
安裝依賴包。
[root@localhost ~]# yum install postgresql-devel [root@localhost ~]# yum -y install pcre-devel [root@localhost ~]# yum -y install openssl openssl-devel
編譯安裝。
[root@localhost ~]# openresty-1.19.3.1 [root@localhost ~]# ./configure --prefix=/beixi/application/openresty-1.19.3.1 \ --with-luajit \ --without-http_redis2_module \ --with-http_iconv_module \ --with-http_postgres_module [root@localhost ~]# make -j2 [root@localhost ~]# make install [root@localhost ~]# ln -sv /beixi/application/openresty-1.19.3.1 /beixi/application/openresty
Openresty(NGINX)主配置文件。
[root@localhost ~]# cat /beixi/application/openresty/nginx/conf/nginx.conf user root; worker_processes 16; events { worker_connections 10240; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '{ "time_local": "$time_local", ' '"remote_addr": "$remote_addr", ' '"referer": "$http_referer", ' '"request": "$request", ' '"status": $status, ' '"bytes": $body_bytes_sent, ' '"agent": "$http_user_agent", ' '"x_forwarded": "$http_x_forwarded_for", ' '"up_addr": "$upstream_addr",' '"up_host": "$upstream_http_host",' '"upstream_time": "$upstream_response_time",' '"request_time": "$request_time"' ' }'; gzip on; gzip_http_version 1.0; gzip_disable 'MSIE[1-6].'; gzip_types text/css text/javascript application/javascript image/jpeg image/png image/gif; gzip_buffers 4 8k; gzip_min_length 1k; gzip_comp_level 9; gzip_vary on; gzip_proxied off; include conf.d/*.conf; }
Openresty(NGINX)子配置文件。
[root@localhost ~]# cat /beixi/application/openresty/nginx/conf/conf.d/image.conf server { listen 8092; server_name localhost; client_max_body_size 20M; location ~ /group1/M00/ { default_type 'text/html'; access_by_lua_file /beixi/application/openresty/nginx/conf/conf.d/token.lua; #lua腳本位置。 proxy_pass http://192.168.101.222; #圖片真實訪問地址 } location /lua { default_type 'text/plain'; content_by_lua 'ngx.say("hello,lua")'; } access_log logs/access.log main; }
token驗證Lua腳本。
[root@localhost ~]# cat /beixi/application/openresty/nginx/conf/conf.d/token.lua local var = ngx.var local arg = ngx.req.get_uri_args() local cjson = require("cjson") local token = nil --在table的agr中獲取token, token字符串組合成的key值 if arg ~= nil and arg.token ~= nil and arg.token ~= '' then --token = "token"..arg.token -- 此處是將url中token的值前面加上token的字符串 token = arg.token -- 獲取token值,token值 等於 Redis中的key end --連接redis local redis = require "resty.redis" local red = redis:new() red:set_timeout(1000) -- 1 sec local ok, err = red:connect("192.168.101.222", 6379) -- Redis服務器ip地址,及端口。 if not ok then ngx.say("failed to connect: ", err, "<br/>") return end -- 請注意這里 auth 的調用過程 這是redis設置密碼的 local count count, err = red:get_reused_times() if 0 == count then ok, err = red:auth("bmbOhi4byBmicF1J") --Redis密碼 if not ok then ngx.say(cjson.encode({code = 500,message = err})) return end elseif err then ngx.say("failed to get reused times: ", err, "<br/>") return end --redis中若 key 存在返回 1 ,否則返回 0 。 local loginfailobj = {code = 500,message = "token is wrong"} local loginfailjson = cjson.encode(loginfailobj) local limitFlag = true if token then local res, errs = red:exists(token) --此處是判斷這個key在redis中是否存在,不判斷value if res == 1 then limitFlag = nil end end if limitFlag then ngx.header["Content-Type"] = "application/json;charset=UTF-8" ngx.say(loginfailjson) ngx.exit(200) end
瀏覽器訪問驗證。
http://192.168.101.211:8092/group1/M00/00/08/wKhl3l_KK7GAQOIsAAC1fqEhRPo876.jpg?token=7bdb621593dd4131bcf57048646a96b5