nginx.conf配置文件
#user nobody; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; log_format log_resp_body '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$request_time $bytes_sent $request_length "$request_body" "$resp_body"'; lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua 模块 lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c模块 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; server_tokens off; #gzip on; # lua #lua_shared_dict limit 10m; #lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua"; #init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua"; #access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua"; server { listen 80; server_name 192.168.0.174; #charset koi8-r; access_log logs/host.access.log log_resp_body; lua_need_request_body on; set $resp_body ""; body_filter_by_lua ' local resp_body = string.sub(ngx.arg[1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered end '; location / { root html; index index.html index.htm; } location =/hello { default_type 'text/plain'; content_by_lua 'ngx.say("欢迎使用Lua")'; } location /lua { default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/test.lua; } location /lua_var { default_type 'text/plain'; content_by_lua_block { ngx.say(ngx.var.arg_b) } } location /lua_request { default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_request.lua; } location /lua_response { default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_response.lua; } location = /lua_log{ default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_log.lua; } location /lua_sum { internal; content_by_lua_block { local args = ngx.req.get_uri_args() ngx.say(tonumber(args.a) + tonumber(args.b)) } } location = /lua_sum_test { content_by_lua_block { local res = ngx.location.capture("/lua_sum", {args={a=3, b=8}}) ngx.say("status:", res.status, " response:", res.body) } } location /lua_redirect { default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_redirect.lua; } location = /favicon.ico { log_not_found off; access_log off; } location ~ /lua_cjson { default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/test_cjson.lua; } location /lua_mysql { default_type 'text/html'; content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/test_mysql.lua; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
test_mysql.lua文件
local function close_db(db) if not db then return end db:close() end -- 1.使用浏览器请求连接,查询指定数据库中的值 -- 2.把从数据库中查询的数据包装成json返回 -- 3.根据浏览器连接中的参数从数据库中查询值,并包装成json返回 local mysql = require("resty.mysql") local cjson = require("cjson") local db, err = mysql:new() if not db then ngx.say("new mysql error : ", err) return end db:set_timeout(1000) local props = { host = "192.168.0.254", port = 3306, database = "hkd", user = "hkd", password = "Hkd123456;" } local res, err, errno, sqlstate = db:connect(props) if not res then ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end -- 获取get参数,http://192.168.0.175/lua_mysql?name=111 local arg = ngx.req.get_uri_args() for k,v in pairs(arg) do local select_sql = string.format("select * from jeecg_demo where %s=%s" ,k,v) -- 字符串格式化 res, err, errno, sqlstate = db:query(select_sql) if not res then ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end end -- 获取post参数 -- 解析 body 参数之前一定要先读取 body ngx.req.read_body() local arg = ngx.req.get_post_args() for k,v in pairs(arg) do ngx.say('111',k,v) local select_sql = string.format("select * from jeecg_demo where %s=%s" ,k,v) -- 字符串格式化 res, err, errno, sqlstate = db:query(select_sql) if not res then ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end end -- 返回从数据库中查询到的json数据 local obj = {} for i, row in pairs(res) do for name, value in pairs(row) do obj[name] = value end local str = cjson.encode(obj) ngx.say(str) end close_db(db)
openresty下lualib目录结构
[root@bogon openresty]# tree lualib/ lualib/ ├── cjson.so ├── librestysignal.so ├── ngx │ ├── balancer.lua │ ├── base64.lua │ ├── errlog.lua │ ├── ocsp.lua │ ├── pipe.lua │ ├── process.lua │ ├── re.lua │ ├── resp.lua │ ├── semaphore.lua │ ├── ssl │ │ └── session.lua │ └── ssl.lua ├── redis │ └── parser.so ├── resty │ ├── aes.lua │ ├── core │ │ ├── base64.lua │ │ ├── base.lua │ │ ├── ctx.lua │ │ ├── exit.lua │ │ ├── hash.lua │ │ ├── misc.lua │ │ ├── ndk.lua │ │ ├── phase.lua │ │ ├── regex.lua │ │ ├── request.lua │ │ ├── response.lua │ │ ├── shdict.lua │ │ ├── time.lua │ │ ├── uri.lua │ │ ├── utils.lua │ │ ├── var.lua │ │ └── worker.lua │ ├── core.lua │ ├── dns │ │ └── resolver.lua │ ├── limit │ │ ├── conn.lua │ │ ├── count.lua │ │ ├── req.lua │ │ └── traffic.lua │ ├── lock.lua │ ├── lrucache │ │ └── pureffi.lua │ ├── lrucache.lua │ ├── md5.lua │ ├── memcached.lua │ ├── mysql.lua │ ├── random.lua │ ├── redis.lua │ ├── sha1.lua │ ├── sha224.lua │ ├── sha256.lua │ ├── sha384.lua │ ├── sha512.lua │ ├── sha.lua │ ├── shell.lua │ ├── signal.lua │ ├── string.lua │ ├── upload.lua │ ├── upstream │ │ └── healthcheck.lua │ └── websocket │ ├── client.lua │ ├── protocol.lua │ └── server.lua └── tablepool.lua