1.下载drizzle模块
地址见: http://agentzh.org/misc/nginx/drizzle7-2011.07.21.tar.gz
1.安装drizzle
- cd /usr/local/openresty
- tar zxvf drizzle7-2011.7.21.tar.gz
- cd drizzle7-2011.7.21/
- ./configure --with-server
- make libdrizzle-1.0
- make install-libdrizzle-1.0
3.编译drizzle-nginx-module
wget http://openresty.org/download/ngx_openresty-1.9.7.4.tar.gz
tar -zxvf ngx_openresty-1.9.7.4.tar.gz
cd ngx_openresty-1.9.7.4
输入nginx -V命令,将参数添加上来即可
3.编译命令
./configure --prefix=/usr/local/openrest --with-cc-opt='-O2 -I/usr/local/Cellar/openssl/1.0.2h_1/include/ -I/usr/local/Cellar/pcre/8.39/include/' --with-stream --with-stream_ssl_module --add-module=/usr/local/openresty/stream-lua-nginx-module --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/Cellar/openssl/1.0.2h_1/lib/ -L/usr/local/Cellar/pcre/8.39/lib/' --with-http_ssl_module --with-http_drizzle_module
make
make install
4.下图表示已经编译成功
4
5.测试一下:
nginx.conf文件:(server位于http中)
运行结果如下:返回格式为json
测试情形二:
server{
listen 8080;
location @user-by-name {
set_unescape_uri $name $arg_name; #接收name参数
set_quote_sql_str $name; #编码防止注入
drizzle_query 'select * from user01 where name=$name';
drizzle_pass backend;
rds_json on;
}
location @user-by-id {
set_quote_sql_str $id $arg_id;
drizzle_query 'select * from user01 where id=$id';
drizzle_pass backend;
rds_json on;
}
location =/user {
access_by_lua '
if ngx.var.arg_name then
return ngx.exec("@user-by-name")
end
if ngx.var.arg_id then
return ngx.exec("@user-by-id")
end
';
rds_json_ret 400 "expecting \"name\" or \"id\" query arguments";
}
}}
运行结果:
测试情形三:
location /mysql-status {
drizzle_status;
}
测试情形四:
location ~ '^/mysql/(.*)' {
set $name $1;
set_quote_sql_str $quote_name $name;
set $sql "SELECT * FROM user01 WHERE username=$quote_name";
drizzle_query $sql;
drizzle_pass backend;
rds_json on;
}
运行结果: