nginx+lua学习笔记

一. 本地linux环境搭建

1. 安装vmware

2. 安装centos http://wiki.centos.org/Download

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 "http://openresty.org/download/ngx_openresty-1.5.8.1.tar.gz"

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;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

 

四. nginx操作mysql

1. 修改nginx.conf
worker_processes  1;
error_log  /log/nginx/logs/error.log;

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  /log/nginx/logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    lua_package_path "/usr/local/openresty/nginx/conf/lua/?.lua;;";

    server {
        listen       8000;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location = /interface{
            default_type 'text/plain';
            content_by_lua_file conf/lua/interface.lua;
        }
    }

}
 
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
        end
        db:set_timeout(6000)
        local ok, err, errinfo, sqlstate = db:connect{
            host = "xxx",
            port = xxx,
            database = "xxx",
            user = "xxx",
            password = "xxx",
            max_packet_size = 1024 * 1024
        }

        if not ok then
            ngx.log(ngx.ERR, "failed to connect: ", err, ": ", errno, " ", sqlstate)
            return false, nil
        end
        ngx.ctx.dbCon = db
    end
    return true, ngx.ctx.dbCon
end

function 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
    end
end
return dbUtil
---------interface.lua
local dbutil  = require("dbutil")
local args = ngx.req.get_uri_args()

if args == nil then
    ngx.say("{}")
end

local md5 = args["md5"]
if not md5 then
    ngx.exit(400)
elseif string.len(tostring(md5)) ~= 32 then
    ngx.exit(400)
end

if not cjson then cjson = require "cjson" end

local status, db = dbutil.getCon()
if not status then
        ngx.exit(500)
end

local sql = "SELECT xxx FROM xxx 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)
end

dbutil.closeCon(db)
 

你可能感兴趣的:(nginx)