參考
http://blog.csdn.net/vboy1010/article/details/7892120
http://www.zhangjixuem.com.cn/2014/4/1/01037.html
https://github.com/bigplum/lua-resty-mongol
安裝:
下載ngx_openresty-1.7.2.1.tar.gz
./configure --prefix=/data/nginx/openresty/resty --with-luajit
make
make install
修改nginx.conf
注意default_type text/plain;
否則瀏覽器觸發是下載
charset utf-8,gbk;
否則可能會亂碼
Java代碼 worker_processes 1; events { worker_connections 1024; } http { include mime.types; charset utf-8,gbk; # default_type application/octet-stream; default_type text/plain; lua_package_path "/data/www/lua/?.lua;;"; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; lua_code_cache off; location /lua { content_by_lua_file /data/www/lua/test.lua; } location /lua_mongo { content_by_lua_file /data/www/lua/test_mongo.lua; } location /lua_get { content_by_lua_file /data/www/lua/test_get.lua; } location / { root html; index index.html index.htm; } # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
Java代碼 local redis = require "resty.redis" local cache = redis.new() local ok, err = cache.connect(cache, '127.0.0.1', '6379') cache:set_timeout(60000) if not ok then ngx.say("failed to connect:", err) return end res, err = cache:set("dog", "an aniaml") if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", res) local res, err = cache:get("dog") if not res then ngx.say("failed to get dog: ", err) return end if res == ngx.null then ngx.say("dog not found.") return end ngx.say("dog: ", res) local ok, err = cache:close() if not ok then ngx.say("failed to close:", err) return end
結果
Java代碼 [root@VM_192_107_centos lua]# !curl curl http://localhost/lua set result: OK dog: an aniaml [root@VM_192_107_centos lua]#
#---------------mongo的基本操作--------------
http://wenku.baidu.com/link?url=K2rmB_5ypVHErZPvi1UucFXfnfEXk4IrvgSQzeSabKJx50W_gpD2cFvCEPQZm0sZgMvHGJTmZahK96Ee3n7OgZTb4gHgybQdZsQ2xGV4nZm
啟動mongo
./mongod --dbpath=../data/db --logpath=../log/mongodb.log
Java代碼 show dbs use admin show collections db.startup_log.count() db.startup_log.find() db.startup_log.find().forEach(function(doc){print(tojson(doc));}); u={name:"haoning",age:21} db.haoning.insert(u) db.haoning.insert({name:"ningning",age:10}) db.haoning.find({name:"haoning"}); db.haoning.find({age:18,name:"ning"}); db.haoning.find().sort({age:1}); db.haoning.find().sort({age:-1}); db.haoning.find().limit(2); db.haoning.find().skip(2).limit(2); db.haoning.find({age:{$gt:9,$lt:20},name:"ning"}); db.haoning.find({age:{$gt:9,$lt:20}}); $gte $lte $ne db.haoning.find({age:{$in:[10,18]}}); db.haoning.find({$or:[{age:10},{age:21}]}); db.haoning.update({name:'ning'},{$set:{age:100,sex:0}}); db.haoning.update({name:'haohao'},{$inc:{age:10}},false,true); db.haoning.findOne({name:"ningning"}); id=db.haoning.findOne({name:"ningning"})._id db.haoning.remove(id); db.haoning.ensureIndex({name:1}) db.haoning.ensureIndex({name:1},{backgrand:true}) db.haoning.ensureIndex({name:1},{unique:true}) db.haoning.ensureIndex({created_at:-1}) db.haoning.ensureIndex({name:1,created_at:-1}) db.haoning.distinct("name");
mongo的安裝
git clone https://github.com/bigplum/lua-resty-mongol.git
make PREFIX=/data/nginx/openresty/resty install
/data/nginx/openresty/resty 為已經安裝的resty的安裝路徑
會在/data/nginx/openresty/resty/lualib/resty
下面添加mongol的一些lua指令碼
mongo的test的lua指令碼:
參考
http://www.zhangjixuem.com.cn/2014/4/1/01037.html
Java代碼 local mongo = require "resty.mongol" local conn = mongo:new() conn:set_timeout(1000) local ok, err = conn:connect("127.0.0.1",27017) if not ok then ngx.say("connect failed: "..err) end local db=conn:new_db_handle("test") local col = db:get_col("test") local r = col:find_one({name="dog"},{_id=0}) for k,v in pairs(r) do ngx.say(k..": "..v) end
mongo的測試
Java代碼 [root@VM_192_107_centos lua]# mongo MongoDB shell version: 2.6.6 connecting to: test > use test switched to db test > db.test.insert({name:"dog"}) WriteResult({ "nInserted" : 1 }) > ^C bye [root@VM_192_107_centos lua]# ^C [root@VM_192_107_centos lua]# !curl curl http://localhost/lua set result: OK dog: an aniaml [root@VM_192_107_centos lua]# curl http://localhost/lua_mongo name: dog [root@VM_192_107_centos lua]#
另外,nginx向lua傳值
Java代碼 local request_method = ngx.var.request_method local args = nil local param = nil local param2 = nil if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() args = ngx.req.get_post_args() end param = args["p"] ngx.say("request: ", param)
設定檔:
Java代碼 location /lua_get { content_by_lua_file /data/www/lua/test_get.lua; }
測試
Java代碼 [root@VM_192_107_centos lua]# !curl curl http://localhost/lua_mongo name: dog [root@VM_192_107_centos lua]#
[root@VM_192_107_centos sbin]# curl -d "p='bbb'" http://127.0.0.1/lua_get?
post
request: 'bbb'
[root@VM_192_107_centos sbin]#
參考http://www.server110.com/nginx/201310/2800.html
#-----------------使用request的 data_body,及json的參數--------
[root@VM_192_107_centos lualib]# ls
cjson.so rds redis resty
[root@VM_192_107_centos lualib]# pwd
/data/nginx/openresty/resty/lualib
看下面有個cjson.so
就是可以require cjson了哈
Java代碼 local json = require("cjson") local request_method = ngx.var.request_method local args = nil local param = nil local param2 = nil --擷取參數的值 if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() args = ngx.req.get_post_args() end param = args["param"] param2 = args["param2"] --升級版(能處理content-type=multipart/form-data的表單): local function explode ( _str,seperator ) local pos, arr = 0, {} for st, sp in function() return string.find( _str, seperator, pos, true ) end do table.insert( arr, string.sub( _str, pos, st-1 ) ) pos = sp + 1 end table.insert( arr, string.sub( _str, pos ) ) return arr end local args = {} local file_args = {} local is_have_file_param = false local function init_form_args() local receive_headers = ngx.req.get_headers() local request_method = ngx.var.request_method if "GET" == request_method then args = ngx.req.get_uri_args() ngx.say("request get: ", args) elseif "POST" == request_method then ngx.say(