OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台.并发性能可在10k-1000k
OpenResty安装
1.添加仓库执行命令
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
2.执行安装
yum install openresty
3.安装成功后 会在默认的目录如下:
/usr/local/openresty
默认已经安装好了nginx,在目录:/usr/local/openresty/nginx 下。
修改/usr/local/openresty/nginx/conf/nginx.conf ,将配置文件使用的根设置为root,目的就是将来要使用lua脚本的时候 ,直接可以加载在root下的lua脚本。
#user nobody; 配置文件第一行原来为这样, 现改为下面的配置
user root root;
缓存预热和二级缓存查询
在/root/lua目录下创建ad_load.lua ,实现连接mysql 查询数据 并存储到redis中。
ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local mysql = require("resty.mysql")
local uri_args = ngx.req.get_uri_args()
local position = uri_args["position"]local db = mysql:new()
db:set_timeout(1000)
local props = { host = "192.168.41.188", port = 3306, database = "changgou_business", user = "root", password = "123456"
}local res = db:connect(props)
local select_sql = "select url,image from tb_ad where status ='1' and position='"..position.."' and start_time<= NOW() AND end_time>= NOW()"
res = db:query(select_sql)
db:close() local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)local ip ="192.168.41.188"
local port = 6379
red:connect(ip,port)red:set("ad_"..position,cjson.encode(res))
red:close()ngx.say("{flag:true}")
修改/usr/local/openresty/nginx/conf/nginx.conf文件:
./nginx -s reload