nginx lua學習筆記

2021-09-01 22:26:15 字數 2539 閱讀 4371

一. 本地linux環境搭建

1. 安裝vmware

2. 安裝centos

3. centos關閉防火牆(當然只是方便學習用才關閉)/etc/init.d/iptables stop

4. 新增使用者 useradd test

passwd test

5. 配置ssh

vim /etc/ssh/sshd_config

service sshd restart

二. 安裝nginx(openresty)

wget ""

tar xzvf ngx_openresty-1.5.8.1.tar.gz

yum install gcc

yum install readline-devel pcre-devel openssl-devel

./configure

gmake

gmake install

三. 配置nginx

path=/usr/local/openresty/nginx/sbin:$path

export path

vim /usr/local/openresty/nginx/conf/nginx.conf

比如:worker_processes  1;

四. nginx操作mysql

1. 修改nginx.conf

}2. 編寫lua指令碼

-------dbutil.lua

dbutil = {}

function dbutil.getcon()

if not ngx.ctx.dbcon then

local mysql = require "resty.mysql"

local db, err = mysql:new()

if not db then

ngx.log(ngx.err, "failed to instantiate mysql: ", err)

return false, nil

enddb:set_timeout(6000)

local ok, err, errinfo, sqlstate = db:connect

if not ok then

ngx.log(ngx.err, "failed to connect: ", err, ": ", errno, " ", sqlstate)

return false, nil

endngx.ctx.dbcon = db

endreturn true, ngx.ctx.dbcon

endfunction dbutil.closecon(dbcon)

if not dbcon then return end

local ok, err = dbcon:set_keepalive(10000, 3000)

if not ok then

ngx.say("failed to set keepalive: ", err)

dbcon:close()

return

endend

return dbutil

---------inte***ce.lua

local dbutil  = require("dbutil")

local args = ngx.req.get_uri_args()

if args == nil then

ngx.say("{}")

endlocal md5 = args["md5"]

if not md5 then

ngx.exit(400)

elseif string.len(tostring(md5)) ~= 32 then

ngx.exit(400)

endif not cjson then cjson = require "cjson" end

local status, db = dbutil.getcon()

if not status then

ngx.exit(500)

endlocal sql = "select *** from *** where md5='" .. md5 .. "'"

local res, err, errno, sqlstate = db:query(sql)

if res then

ngx.say(cjson.encode(res))

else

ngx.say(err, errno,sqlstate)

enddbutil.closecon(db)

Nginx Lua學習筆記 環境搭建

使用的openresty 具體教程可以參考 這裡只記錄下我自己的環境搭建過程 系統使用的是ubuntu 14.04.1 server amd64.iso 所有操作都使用了sudo提權.首先參考解決apt get install時media change please insert the disc ...

nginx lua 學習記錄

記錄一下環境搭建過程dl dl workspace eotu nginx ls al 總用量 44 drwxrwxr x 11 dl dl 4096 oct 9 17 32 drwxrwxr x 31 dl dl 4096 oct 16 15 45 drwxrwxr x 4 dl dl 4096 o...

nginx lua開發例子

參考文章 conf檔案與原來文章的配置有點不同,這個要參考官方文件 vim usr chapter6 nginx chapter6.conf upstream backend server location ad d lua檔案 local redis require resty.redis loc...