首页 > 系统相关 >openresty(nginx)、lua、drizzle测试

openresty(nginx)、lua、drizzle测试

时间:2023-08-11 15:32:59浏览次数:38  
标签:http name lua -- nginx openresty mysql ngx 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; 
    #access_log  logs/host.access.log  main; 
        root   html; 
        index  index.html index.htm; 
    } 
        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; 
    }  
            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; 
    } 
        set_quote_sql_str $id $arg_id; 
        drizzle_query 'select * from cats where id=$id'; 
        drizzle_pass backend; 
        rds_json on; 
    } 
        access_by_lua ' 
            if ngx.var.arg_name then 
                return ngx.exec("@cats-by-name") 
            end 
                return ngx.exec("@cats-by-id") 
            end 
        '; 
    } 
    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; 
    } 
    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 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' 
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: 32261upstream 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模块 

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 第三方模块-漫谈缘起 

CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据 

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安装配置详解 

 

nginx安装 

标签:http,name,lua,--,nginx,openresty,mysql,ngx,drizzle
From: https://blog.51cto.com/u_6186189/7048502

相关文章

  • nginx or apache前端禁收录,爬虫,抓取
    一、Nginx规则直接在server 中新增如下规则即可:##################################################禁止蜘蛛抓取动态或指定页面规则By##################################################server{listen80;server_namezhangge.net;indexindex.htmlindex.......
  • #yyds干货盘点#nginx中fastcgi_params文件及相应配置
    在ubuntu服务器安装完php7.4-fdm和nginx后,发现fastcgi_params没有生成,也可能是二次安装的关系。所以临时去网上找了个手工建上。特意在这里记录下,避免下次再遇到同样的问题。#脚本文件请求的路径,也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如......
  • centos7.X安装nginx – 东凭渭水流
    1.安装nginx需要使用root用户2.配置nginx源 rpm-ivhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm #运行如下 [root@localhost~]#rpm-ivhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0......
  • Jenkins +nginx 搭建前端构建环境
    欢迎访问幸福拾荒者,一个前端知识总结分享平台,与大家一起共同成长共同进步!......
  • Nginx日志分析- AWK命令快速分析日志--封禁访问请求最多、最频繁的恶意ip
    Nginx日志常用分析命令示范(注:日志的格式不同,awk取的项不同。下面命令针对上面日志格式执行)1.分析日志的方法1)总请求数cd/usr/local/nginx/logs/wc-laccess.log|awk'{print$1}'166252)独立IP数awk'{print$1}'access.log|sort|uniq|wc-l4003)每秒客户端......
  • 使用awk分析nginx访问日志access.log
    1.awk简介awk是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk的处理文本和数......
  • Python处理Nginx配置的实现方法
    Nginx是一个高性能的Web服务器和反向代理服务器,它可以用于实现多种功能。在实际应用中,我们可能需要根据不同的需求修改Nginx的配置文件。本文将介绍如何使用Python来处理Nginx配置文件。一、安装必要的库为了方便地操作Nginx配置文件,我们需要安装一些Python库。其中,pyparsing和ngin......
  • Nginx配置防盗链(详细了解如何配置nginx防盗链)
     worker_processes1;#允许进程数量,建议设置为cpu核心数或者auto自动检测,注意Windows服务器上虽然可以启动多个processes,但是实际只会用其中一个events{#单个进程最大连接数(最大连接数=连接数*进程数)#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100......
  • Nginx+keepalived主从双机热备自动切换解决方案
    Nginx+keepalived主从双机热备自动切换解决方案测试环境如下:系统:Ceentos6.464位主nginx服务器:192.168.122.5备nginx服务器:192.168.122.6VIP:192.168.122.15一、Nginx+keepalived安装—脚本安装#!/bin/bash#author:kuangl#mail:[email protected]#description:The......
  • Linux下搭建Nginx+nginx-rtmp-module流媒体服务器
    今天我们使用的是linux系统为Centos64位服务器。下载安装nginx首先新建nginx目录存放nginx:mkdirnginx1然后进入nginx目录分别下载nginx及nginx-rtmp-module:进入nginx目录cdnginx下载nginxwgethttp://nginx.org/download/nginx-1.17.9.tar.gz下载nginx-rtmp-modulehttps://codel......