openresty(nginx)、lua、drizzle测试

2023-10-13 00:20

本文主要是介绍openresty(nginx)、lua、drizzle测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、概述:

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/ 
./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', '6379')
local res = cache:get("foo")
if res==ngx.null thenngx.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 thenngx.say("failed to instantiate mysql: ", err)return
enddb: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 thenngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)return
endngx.say("connected to mysql.")local res, err, errno, sqlstate =db:query("drop table if exists cats")
if not res thenngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")return
endres, err, errno, sqlstate =db:query("create table cats ".. "(id serial primary key, ".. "name varchar(5))")
if not res thenngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")return
endngx.say("table cats created.")res, err, errno, sqlstate =db:query("insert into cats (name) ".. "values (\'Bob\'),(\'\'),(null)")
if not res thenngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")return
endngx.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 thenngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")return
endlocal 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 thenngx.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测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/199438

相关文章

Nginx如何进行流量按比例转发

《Nginx如何进行流量按比例转发》Nginx可以借助split_clients指令或通过weight参数以及Lua脚本实现流量按比例转发,下面小编就为大家介绍一下两种方式具体的操作步骤吧... 目录方式一:借助split_clients指令1. 配置split_clients2. 配置后端服务器组3. 配

Nginx实现前端灰度发布

《Nginx实现前端灰度发布》灰度发布是一种重要的策略,它允许我们在不影响所有用户的情况下,逐步推出新功能或更新,通过灰度发布,我们可以测试新版本的稳定性和性能,下面就来介绍一下前端灰度发布的使用,感... 目录前言一、基于权重的流量分配二、基于 Cookie 的分流三、基于请求头的分流四、基于请求参数的分

一文详解Nginx的强缓存和协商缓存

《一文详解Nginx的强缓存和协商缓存》这篇文章主要为大家详细介绍了Nginx中强缓存和协商缓存的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、强缓存(Strong Cache)1. 定义2. 响应头3. Nginx 配置示例4. 行为5. 适用场景二、协商缓存(协

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

Ubuntu 22.04 服务器安装部署(nginx+postgresql)

《Ubuntu22.04服务器安装部署(nginx+postgresql)》Ubuntu22.04LTS是迄今为止最好的Ubuntu版本之一,很多linux的应用服务器都是选择的这个版本... 目录是什么让 Ubuntu 22.04 LTS 变得安全?更新了安全包linux 内核改进一、部署环境二、安装系统

Keepalived+Nginx双机配置小结

《Keepalived+Nginx双机配置小结》本文主要介绍了Keepalived+Nginx双机配置小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1.1 软硬件要求1.2 部署前服务器配置调优1.3 Nginx+Keepalived部署1.3

nginx upstream六种方式分配小结

《nginxupstream六种方式分配小结》本文主要介绍了nginxupstream六种方式分配小结,包括轮询、加权轮询、IP哈希、公平轮询、URL哈希和备份服务器,具有一定的参考价格,感兴趣的可... 目录1 轮询(默认)2 weight3 ip_hash4 fair(第三方)5 url_hash(第三