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
