一、概述:
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 drizzle7-2011.07.21.tar.gz
cd drizzle7-2011.07.21/
./configure --without-server
make libdrizzle-1.0
make install-libdrizzle-1.0
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_redis { location /lua_mysql { location @cats-by-name { 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" |
/usr/local/lua_test/mysql_test.lua |
local mysql = require "resty.mysql" |
五、验证结果
curl测试 |
$ curl 'http://127.0.0.1/lua_test' $ redis-cli set foo 'hello,lua-redis' $ curl 'http://127.0.0.1/lua_redis' hello,lua-redis $ curl 'http://127.0.0.1/lua_mysql' connected to mysql. table cats created. 3 rows inserted into table cats (last insert id: 1) result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}] $ curl 'http://127.0.0.1/cats' {"errcode":400,"errstr":"expecting \"name\" or \"id\" query arguments"} $ curl 'http://127.0.0.1/cats?name=bob' [{"id":1,"name":"Bob"}] $ curl 'http://127.0.0.1/cats?id=2' [{"id":2,"name":""}] $ curl 'http://127.0.0.1/mysql/bob' [{"id":1,"name":"Bob"}] $ curl 'http://127.0.0.1/mysql-status' worker process: 32261 upstream backend connection pool capacity: 0 servers: 1 peers: 1 |
六、参考资料
1.openresty http://openresty.org/cn/index.html
2.tengine http://tengine.taobao.org/documentation_cn.html
如何安装nginx_lua_module模块
http://www.cnblogs.com/yjf512/archive/2012/03/27/2419577.html
nginx+lua 项目使用记(二)
http://blog.chinaunix.net/uid-26443921-id-3213879.html
nginx_lua模块基于mysql数据库动态修改网页内容
https://www.centos.bz/2012/09/nginx-lua-mysql-dynamic-modify-content/
突破log_by_lua中限制Cosocket API的使用
http://17173ops.com/2013/11/11/resolve-cosocket-api-limiting-in-log-by-lua.shtml
17173 Ngx_Lua使用分享
http://17173ops.com/2013/11/01/17173-ngx-lua-manual.shtml
关于 OPENRESTY 的两三事
http://zivn.me/?p=157
Nginx_Lua
http://www.ttlsa.com/nginx/nginx-lua/
Nginx 第三方模块-漫谈缘起
http://www.cnblogs.com/yjf512/archive/2012/03/30/2424726.html
CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据
http://www.cnblogs.com/kgdxpr/p/3550633.html
Nginx与Lua
http://huoding.com/2012/08/31/156
由Lua 粘合的Nginx生态环境
http://blog.zoomquiet.org/pyblosxom/oss/openresty-intro-2012-03-06-01-13.html
Nginx 第三方模块试用记
http://chenxiaoyu.org/2011/10/30/nginx-modules.html
agentzh 的 Nginx 教程(版本 2013.07.08)
http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
CentOS下Redis 2.2.14安装配置详解
http://www.cnblogs.com/hb_cattle/archive/2011/10/22/2220907.html
nginx安装
http://blog.csdn.net/gaojinshan/article/details/37603157