Introduction
The company needs to do a similar webmaster statistics project, gave a lot of programs; one scheme is to use Ngx_lua (openresty) call MongoDB to do log storage; Before the project started, I studied the feasibility of this program, write a demo.
1. Prepare
(1) Read the openresty should know, Openresty only provide Redis module modules, no MongoDB driver module (do not know openresty; you can see the openresty best implementation); So we have to provide a driver here first. , went to the Chinese forum and found several good libarary projects that combine LUA with MongoDB:
On the GitHub, there are Lua-resty-mongol, Wuxianglong Lua-mongo and other projects; After considering it, I intend to use the Lua-resty-mongol project. Because the project itself is designed to be compatible with Openresty.
(2) This demo itself is based on openresty, so you need to install Openresty on your system, I use the CentOS 6.5 of the system, as for the specific download installation, here is not introduced in detail; If you need to see this article http:/ /www.52yunwei.cn/?p=415;
(3) Previously mentioned Lua-resty-mongol project, we want to download the project under the GitHub, download and compile,
Make install
Unpack the unpacked packets into the lualib/resty/
If your openresty default installation is under/usr/local/openresty/lialib, my side of the path is under the Opt/openresty/lualib/resty;
(4) is the corresponding MongoDB service, my MongoDB service here is in their own virtual machine, if not, first go to the official website to download a MongoDB, and then start
This is not covered in detail here.
2. Examples
(1). Before the Lua-resty-mongol project also has a configuration item, which is to configure the default initialization path of your Mongol in nginx.conf
# previously said, if you are the default openresty words here can be configured is/usr/local/openresty/lialib/?/#
Lua_package_path '/opt/ Opentresty/lualib/?/init.lua;; ';
(2). For NGX, the entire nginx.conf configuration is as follows
User Nginx;
Worker_processes 4;
Pid/opt/logs/nginx/nginx.pid;
Error_log/opt/logs/nginx/error.log;
events {use Epoll;
Worker_connections 10240;
} http {include mime.types;
#default_type ' text/html ';
#定义日志格式 #default_type ' Application/octet-stream ';
#指定lua_mongol Initialize the default path Default_type ' Text/plain ';
Lua_package_path '/opt/opentresty/lualib/?/init.lua;; ';
CharSet Utf-8;
Error_log/opt/logs/nginx/error.log;
Access_log off; #log_format Main ' $remote _addr\t$uid_got$uid_set\t$http_host\t$time_iso8601\t$request\t$status\t$body_bytes_sent
\t$http_referer\t$request_time\t$http_user_agent '; #log_format Tick ' $msec ^a$remote_addr^a$u_domain^a$u_url^a$u_title^a$u_referrer^a$u_sh^a$u_sw^a$u_cd^a$u_lang^a$
Http_user_agent^a$u_utrace^a$u_account '; LoG_format Tick ' $msec ^a^ $remote _addr ^a^ $u _domain ^a^ $u _url ^a^ $u _title ^a^ $u _referrer ^a^ $u _sh ^a^ $u _sw ^a^ $u _cd
^a^ $u _lang ^a^ $http _user_agent ^a^ _utrace $u ^a^ ' $u _account ';
Client_max_body_size 100m;
Sendfile on;
Keepalive_timeout 60;
Fastcgi_intercept_errors on;
Proxy_connect_timeout 60;
Proxy_send_timeout 90;
Proxy_read_timeout 1800;
Large_client_header_buffers 4 128k;
Proxy_ignore_client_abort on;
gzip on;
Gzip_min_length 10k;
Gzip_buffers 4 16k;
Gzip_comp_level 2; Gzip_types text/plain text/javascript application/javascript application/x-javascript text/css
Application/octet-stream;
Gzip_vary on;
#userid userid on;
Userid_name UUID;
Userid_path/;
Userid_expires Max;
Include _ext.conf; INclude apps/*.conf; }
(3). Configuration required location
from the above is nginx.conf, I can see that the server here is created in the Apps subfolder, I have a apps file named
_mongo.conf configuration file, which is configured in this LUA Call the MongoDB location
server {listen 8081;
server_name 192.168.1.128;
#关闭lua_code Cache Lua_code_cache off;
Location/lua {Content_by_lua_file/opt/openresty/lualib/resty/mongol/test_lua.lua;
} location/lua_mongo {Content_by_lua_file/opt/openresty/lualib/resty/mongol/test_mongol.lua;
} location/lua_test {set $test "Hello World";
#使用acess stage to do access conditions to deal with Access_by_lua ' if (ngx.var.test = = "Hello World") then Ngx.say ("Validate Pass" ...) Ngx.var.test) Else Ngx.log (NGX.
ERR, "Validation failed", "" "End";
#业务处理 Content_by_lua ' ngx.header.content_type = "Text/plain";
Local A, B = 1; Ngx.say (ngx.var.test.
a);
'; }
}
Location Lua_mongo Content_by_lua_file is lua called MongoDB concrete actions
executed under/opt/openresty/lualib/resty/mongol/test_ Mongol.lua file, the contents of the file is:
Local MONGO =require "Resty.mongol" local JSON = require "Cjson"-Get connection Objects local conn =mongo:new () conn:set_timeout (1000)- -Get Connection Client local Ok,err =conn:connect ("192.168.1.128", 27017) if not OK then Ngx.say ("Connect Failed" ...
ERR) End--get database Local db = Conn:new_db_handle ("Leo")-User authorized local OK, err = Db:auth ("Zjf", "zjf123456") if OK then Ngx.say ("User auth success"). OK) end--Get collection Local coll = db:get_col ("Leonardo")--Get document collection Local cursor = Coll:find ({})--json transcoding-function Jso
N_decode (str) Local json_value =nil pcall (function (str) json_value = Json.decode (str) end, str) Return Json_value End--loop for Index,item in Cursor:pairs () do Ngx.say (' data: '. Index) if not item[' url '] then Ngx.say (' Data: ' ... item["title"] Else Ngx.say (' data: '). item["title"]. item[' URL '] Ngx.say (json_decode (item[' url ')) End--Gets a single collection local res =coll:find_one ({key = 1 }) if reS then Ngx.say (res[' title ']) end--Insert collection local bson1 ={title = ' haha ', url = ' www.baidu.com ', key = 300};
--Insert the local docs ={bson1} in table tables; Local Rsok,err =coll:insert (docs,0,0) If Err then Ngx.say (' error--'). ERR) Else Ngx.say (' OK----' ... Rsok) End--delete operation local Deok,err = Coll:delete ({title = ' Hello '},0,0) If Err then Ngx.say (' Delete error--' ... ERR) Else Ngx.say (' Delete ok--' ... DEOK) End--close connection if Conn then Conn:close () end
Here is a complete example to write.
(4). Start the MongoDB service and start the Nginx service
[Root@leoleo apps]# Curl 192.168.1.128:8081/lua_mongo
data: 1
data: haha www.baidu.com
nil
data: 2
data: haha www.baidu.com
nil
data: 3
data: haha www.baidu.com
nil
OK-----1
Delete OK---1
So this demo is written.
3. something * *
Using LUA_NGINX+MONGODB This scheme was finally rejected by our project team, for what reason.
While Lua can be used to write nginx to the MongoDB that collects user actions on a Web page, the LUA direct operation MongoDB has a number of flaws, such as an instance, a connection instance for each write, and a connection The Behavior collection on the web is a high concurrency, and obviously this demo is not satisfying.