一、概述:
1.研究目标:nginx中使用lua脚本,及nginx直接访问MySQL,Redis
2.需要安装的内容:
openresty,mysql,redis
3.OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。http://openresty.org/cn/index.html
二、安装说明
0.环境准备
$yum install -y gcc gcc-c++ readline-devel pcre-devel openssl-devel tcl perl
1、安装drizzle http://wiki.nginx.org/HttpDrizzleModule
cd /usr/local/src/
wget http://openresty.org/download/drizzle7-2011.07.21.tar.gz
tar xzvf drizzle-2011.07.21.tar.gz
cd drizzle-2011.07.21/
&& make libdrizzle-1.0 \
&& make install-libdrizzle-1.0 \
&& ln -s /usr/local/lib/libdrizzle.so.1 /usr/lib/libdrizzle.so.1 \
&& ln -s /usr/local/lib/libdrizzle.so.1 /usr/lib64/libdrizzle.so.1 \
&& popd \
&& rm -rf drizzle7*
&& rm -rf drizzle7*
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
2、安装openresty
wget http://openresty.org/download/ngx_openresty-1.7.2.1.tar.gz
tar xzvf ngx_openresty-1.7.2.1.tar.gz
cd ngx_openresty-1.7.2.1/
./configure --with-http_drizzle_module
gmake
gmake install
三、nginx配置nginx.conf
/usr/local/openresty/nginx/conf/nginx.conf |
# 添加MySQL配置(drizzle) server { #charset koi8-r; location / { location /lua {
location /lua_mysql {
location @cats-by-id { location = /cats { if ngx.var.arg_id then rds_json_ret 400 "expecting \"name\" or \"id\" query arguments"; # 通过url匹配出name,并编码防止注入,最后以json格式输出结果 # 查看MySQL服务状态 |
四、lua测试脚本
/usr/local/lua_test/redis_test.lua |
local redis = require "resty.redis" local cache = redis.new() cache.connect(cache, '127.0.0.1', '6379') local res = cache:get("foo") if res==ngx.null then ngx.say("This is Null") return end ngx.say(res) |
/usr/local/lua_test/mysql_test.lua |
local mysql = require "resty.mysql" local db, err = mysql:new() if not db then ngx.say("failed to instantiate mysql: ", err) return end db:set_timeout(1000) -- 1 sec -- or connect to a unix domain socket file listened -- by a mysql server: -- local ok, err, errno, sqlstate = -- db:connect{ -- path = "/path/to/mysql.sock", -- database = "ngx_test", -- user = "ngx_test", -- password = "ngx_test" } local ok, err, errno, sqlstate = db:connect{ host = "127.0.0.1", port = 3306, database = "test", user = "root", password = "123456", max_packet_size = 1024 * 1024 } if not ok then ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate) return end ngx.say("connected to mysql.") local res, err, errno, sqlstate = db:query("drop table if exists cats") if not res then ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") return end res, err, errno, sqlstate = db:query("create table cats " .. "(id serial primary key, " .. "name varchar(5))") if not res then ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") return end ngx.say("table cats created.") res, err, errno, sqlstate = db:query("insert into cats (name) " .. "values (\'Bob\'),(\'\'),(null)") if not res then ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") return end ngx.say(res.affected_rows, " rows inserted into table cats ", "(last insert id: ", res.insert_id, ")") -- run a select query, expected about 10 rows in -- the result set: res, err, errno, sqlstate = db:query("select * from cats order by id asc", 10) if not res then ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") return end local cjson = require "cjson" ngx.say("result: ", cjson.encode(res)) -- put it into the connection pool of size 100, -- with 10 seconds max idle timeout local ok, err = db:set_keepalive(10000, 100) if not ok then ngx.say("failed to set keepalive: ", err) return end -- or just close the connection right away: -- local ok, err = db:close() -- if not ok then -- ngx.say("failed to close: ", err) -- return -- end '; |
五、验证结果
curl测试 |
$ curl 'http://127.0.0.1/lua_test' $ redis-cli set foo 'hello,lua-redis' $ curl 'http://127.0.0.1/lua_mysql' $ curl 'http://127.0.0.1/cats' $ curl 'http://127.0.0.1/cats?name=bob' $ curl 'http://127.0.0.1/cats?id=2' $ curl 'http://127.0.0.1/mysql/bob' $ curl 'http://127.0.0.1/mysql-status' upstream backend |
参考:
http://blog.csdn.net/pzqingchong/article/details/54929859
http://www.iyunv.com/thread-255809-1-1.html