一、概述:

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)
upstream backend {
    drizzle_server 127.0.0.1:3306 dbname=test user=root password=123456 protocol=mysql;
    drizzle_keepalive max=200 overflow=ignore mode=single;
}

server {
    listen       80;
    server_name  localhost;

#charset koi8-r;
    #access_log  logs/host.access.log  main;

location / {
        root   html;
        index  index.html index.htm;
    }

location /lua {
        default_type text/plain;
        content_by_lua 'ngx.say("hello, lua")';
    }

location /lua_redis {
        default_type text/plain;
        content_by_lua_file /usr/local/lua_test/redis_test.lua;
    }

location /lua_mysql {
            default_type text/plain;
            content_by_lua_file /usr/local/lua_test/mysql_test.lua;
    }

location @cats-by-name {
        set_unescape_uri $name $arg_name;
        set_quote_sql_str $name;
        drizzle_query 'select * from cats where name=$name';
        drizzle_pass backend;
        rds_json on;
    }

location @cats-by-id {
        set_quote_sql_str $id $arg_id;
        drizzle_query 'select * from cats where id=$id';
        drizzle_pass backend;
        rds_json on;
    }

location = /cats {
        access_by_lua '
            if ngx.var.arg_name then
                return ngx.exec("@cats-by-name")
            end

if ngx.var.arg_id then
                return ngx.exec("@cats-by-id")
            end
        ';

rds_json_ret 400 "expecting \"name\" or \"id\" query arguments";
    }

# 通过url匹配出name,并编码防止注入,最后以json格式输出结果
    location ~ '^/mysql/(.*)' {
        set $name $1;
        set_quote_sql_str $quote_name $name;
        set $sql "SELECT * FROM cats WHERE name=$quote_name";
        drizzle_query $sql;
        drizzle_pass backend;
        rds_json on;
    }

# 查看MySQL服务状态
    location /mysql-status {
        drizzle_status;
    }
}

四、lua测试脚本

/usr/local/lua_test/redis_test.lua
local redis = require "resty.redis"
local cache = redis.new()
cache.connect(cache, '127.0.0.1', '')
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() -- 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 = ,
database = "test",
user = "root",
password = "",
max_packet_size = * } 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", )
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(, )
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'
hello, lua

$ redis-cli set foo 'hello,lua-redis'

OK

$ 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

  active connections: 0

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

openresty(nginx)、lua、drizzle调研的更多相关文章

  1. OpenResty(nginx+lua) 入门

    OpenResty 官网:http://openresty.org/ OpenResty 是一个nginx和它的各种三方模块的一个打包而成的软件平台.最重要的一点是它将lua/luajit打包了进来, ...

  2. (转)OpenResty(nginx+lua) 开发入门

    原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/  Open ...

  3. CentOS安装OpenResty(Nginx+Lua)开发环境

    一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...

  4. 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil

    openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request ...

  5. 搭建OpenResty(Nginx+Lua)

    这篇文章是一个多月前写的,当时之所以搭建这个是为了最大程度上发挥Nginx的高并发效率(主要是结合lua脚本),参考的话,主要参考张开涛先生写的跟开涛学Nginx+lua系列文章,地址为:https: ...

  6. OpenResty(Nginx+Lua)开发入门

    Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...

  7. 跟我学OpenResty(Nginx+Lua)开发目录贴 (转)

    使用Nginx+Lua开发近一年的时间,学习和实践了一些Nginx+Lua开发的架构,为了让更多人使用Nginx+Lua架构开发,利用春节期间总结了一份基本的学习教程,希望对大家有用.也欢迎谈探讨学习 ...

  8. 【原创】运维基础之OpenResty(Nginx+Lua)+Kafka

    使用docker部署 1 下载 # wget https://github.com/doujiang24/lua-resty-kafka/archive/v0.06.tar.gz# tar xvf v ...

  9. OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能

    http://www.hopesoft.org/blog/?p=1188 http://www.imagemagick.org/download/ 2.用法 原始图片是input.jpg,尺寸:160 ...

  10. OpenResty部署nginx及nginx+lua

    因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件 ...

随机推荐

  1. 【.NET框架】Dapper ORM 用法—Net下无敌的ORM

    假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载 Dapper的优势: 1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编 ...

  2. Python入门笔记(22):Python函数(5):变量作用域与闭包

    一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除,否则他们存活到脚本运行结束,且对于所有的函数都可访问. 当搜索一个标识符(也称变量.名字等),Pyt ...

  3. CSS3属性transform详解

    在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾斜.移动这四种类型的变形处理,本文将对此做详细介绍. 一.旋转 rotate 用法:transform: rotate(45 ...

  4. mysql学习笔记 第八天

    where,group by,having重新详解 where的用法: where与in的配合使用,in(值1,值2,...)表示结果在值1,值2,...其中任何一个. 聚合函数和group by的用 ...

  5. jquery checkbox checked

    1.question: when first operate the checkbox with sentence like : $("input[type=checkbox]") ...

  6. CentOS 编译安装 MySQL5.7

    下载 所有版本下载地址: http://dev.mysql.com/downloads/mysql/ 此处用 5.7.10 wget http://dev.mysql.com/get/Download ...

  7. [译] 第三十天:Play Framework - Java开发者梦寐以求的框架 - 百花宫

    前言 30天挑战的最后一天,我决定学习 Play Framework .我本来想写Sacla,但是研究几个小时后,我发现没法在一天内公正评价Scala,下个月花些时间来了解并分享经验.本文我们先来看看 ...

  8. 你可以使用 play framework 做5件很爽的事情http://www.anool.net/?p=629

    1.绑定HTTP参数到JAVA方法里的参数. 使用PLAY可以很简单的从JAVA代码中检索HTTP参数.只要把方法参数申明成和HTTP参数相同既可. 比如,这个request: Http代码 /art ...

  9. mybatis hellworld

    用maven来进行搭建项目的~~   1. 搭建环境 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  10. Servlet API遍程常用接口和类

    本文主要总结Servlet  API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...