openresty中文及应用

发布时间:2017-11-24 18:36:38 阅读:1133次

教程

http://wiki.jikexueyuan.com/project/openresty/openresty/log_response.html

http://openresty.org/cn/samples.html

http://jinnianshilongnian.iteye.com/blog/2280928

https://github.com/362228416/openresty-web-dev

[root@localhost html]# cat hello_http.lua
local request_method = ngx.var.request_method
local args = nil
ngx.say('处理htpp请求,get或post请求的参数如下')
if "GET" == request_method then
    ngx.say("get请求")
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.say("post请求")
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
for key, val in pairs(args) do
    if type(val) == "table" then
        ngx.say(key, ": ", table.concat(val, ", "))
    else
        ngx.say(key, ": ", val)
    end
end
ngx.say('get or post request over')


[root@localhost html]# cat redis.lua
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
-- or connect to a unix domain socket file listened
-- by a redis server:
--     local ok, err = red:connect("unix:/path/to/redis.sock")
local ok, err = red:connect("192.168.0.116", 6379)
if not ok then
    ngx.say("failed to connect___: ", err)
    return
end
ok, err = red:set("dog", "an animal")
if not ok then
    ngx.say("failed to set dog: ", err)
    return
end
ngx.say("set result: ", ok)
local res, err = red: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)
red:init_pipeline()
red:set("cat", "Marry")
red:set("horse", "Bob")
red:get("cat")
red:get("horse")
local results, err = red:commit_pipeline()
if not results then
    ngx.say("failed to commit the pipelined requests: ", err)
    return
end
for i, res in ipairs(results) do
    if type(res) == "table" then
        if not res[1] then
            ngx.say("failed to run command ", i, ": ", res[2])
        else
            -- process the table value
        end
    else
        -- process the scalar value
    end
end
-- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(10000, 100)
if not ok then
    ngx.say("failed to set keepalive: ", err)
    return
end
[root@localhost html]#


[root@localhost html]# cat mysql.lua
local mysql = require "resty.mysql"
local db,err = mysql:new()
if not db then
        ngx.say("failed to instantiate mysql: ",err)
        return
end
db:set_timeout(1000)
local ok,err,errno,sqlstate = db:connect{
        host = "192.168.0.116",
        port = 3306,
        database = "phpjx",
        user = "root",
        password = "root",
        max_package_size = 1024
}
if not ok then
        ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
        return
end
res,err,errno,sqlstate = db:query("select id,title from article where id=321")
if not res then
        ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
        return
end
local cjson = require "cjson"
ngx.say(cjson.encode(res))
[root@localhost html]#


[root@localhost conf]# cat  /usr/local/openresty/nginx/conf/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;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
location /lua {
                 default_type 'text/plain';
                 content_by_lua 'ngx.say("Hello,kevin!Iam lua.")';
        }
location /post_1{
    content_by_lua '
        ngx.req.read_body()
        local args = ngx.req.get_post_args()
        for key, val in pairs(args) do
            if type(val) == "table" then
                ngx.say(key, ": ", table.concat(val, ", "))
            else
                ngx.say(key, ": ", val)
            end
        end
    ';
}
location /post_2{
     lua_need_request_body on ;
     default_type 'text/plain' ;
     content_by_lua_file /var/www/html/hello_http.lua ;
}
location /redis_test{
     lua_need_request_body on ;
     default_type 'text/plain' ;
     content_by_lua_file /var/www/html/redis.lua ;
}
location /mysql_test{
     lua_need_request_body on ;
     default_type 'text/plain' ;
     content_by_lua_file /var/www/html/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           /var/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$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;
    #    }
    #}
}
[root@localhost conf]#

OpenResty的现状、趋势、使用及学习方法

https://segmentfault.com/a/1190000004113020

http://tj1.test.com/note/install?code=bcFjsyXNRzEmiMPblEC4NLQ9VSfK/nlrfmZhE1pisQ++d8NP4fJ2w9LJcSCqyel0EW8OzzaLSGIbVwR+9j3S15y3REid5uLG2lOqHo2e2EMrfJ7LCRj7gGrk+GeLtJAENXDk2gsrmvZJXYpsIooJrYSIKPoIFGezn4l9J/YVoxkIgLYEa4OXoRNKmp4ZIdW6oQ8E2DNLT0R3E+qMtgji+GArhXgDdpnXRwErmIL5ezIOv8ZmkQdyiR2Jqs0xAgFEhWmPqSAvGHwcQqf22loGFF2ZTAuAFiy9usxTVqqgMZqc2d0=

http://tj1.test.com/note/uninstall?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJl5gYIO61nQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4Mv9Y=

http://tj1.test.com/note/kunbang?code=bcFjsyXNRzEmiMPblEC4NLQ9VSfK/nlrfmZhE1pisQ++d8NP4fJ2w9LJcSCqyel0EW8OzzaLSGIbVwR+9j3S15y3REid5uLG2lOqHo2e2EMrfJ7LCRj7gGrk+GeLtJAENXDk2gsrmvZJXYpsIooJrYSIKPoIFGezn4l9J/YVoxkIgLYEa4OXoRNKmp4ZJNW6oQ8E2DNLT0R3E+qMtgji+GArhXgDdpnXRwErmIL5ezJV2IUbwWJW6SaSt7BhZ1wSwlKQkD4VBkVy

http://tj1.test.com/note/stat?code=bcFjsyXNRzEmiMPblEC4NLQ9VSfK/nlrfmZhE1pisQ++d8NP4fJ2w9LJcSCqyel0EW8OzzaLSGIbVwR+9j3S15y3REid5uLG2lOqHo2e2EMrfJ7LCRj7gGrk+GeLtJAENXDk2gsrmvZJXYpsIooJrYSIKPoIFGezn4l9J/YVoxkIgLYEa4OXoRNKmp4ZIdW6oQ8E2DNLT0R3E+qMtgji+GArhXgDdpnXRwErmIL5ezIN

http://tj1.test.com/note/updateinstall?code=bcFjsyXNRzEmiMPblEC4NLQ9VSfK/nlrfmZhE1pisQ++d8NP4fJ2w9LJcSCqyel0EW8OzzaLSGIbVwR+9j3S15y3REid5uLG2lOqHo2e2EMrfJ7LCRj7gGrk+GeLtJAENXDk2gsrmvZJXYpsIooJrYSIKPoIFGezn4l9J/YVoxkIgLYEa4OXoRNKmpEbLNW6oQ8E2DNLT0R3E+qMtgji+GArhXgDdpnXRwErmIL5ezIOmMZBkyAKzmfu6LBgZ1wSuxXo931oVyAveqSlmh1JSRT2IkKQDyfD9shdZ/zjO9/I2cBnOS/ArtnFjd6vmOhNdnFJPAo=

http://tj1.test.com/note/active?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJkJsXLOSwnQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4Mv9Y=

http://tj1.test.com/note/online?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJl5gYJ+y2nQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4M

http://tj1.test.com/note/process_run_duration?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJl5gYJ+y2nQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4Mv8dakT8J9W3f4YpZWw==

http://tj1.test.com/note/jingpin?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJl5gYJ+y2nQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4Mv48A1WpbqHnX6YksFwR1izLA92kxBnxzWeGhhEwWFla3UBXVXjXF5spWSeD9bpfR4t45ZS3LspvIjM4=

http://tj1.test.com/note/userclick?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJl5gYJ+m1nQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4Mv4UEyWBYszru6A==

http://tj1.test.com/note/fileoperate?code=bcFjsyXNRzFIjsPdl0ODPvs9J1rPiw4fC2cUEyVmunq5dcZMkvEAwNTMeyjQye9FHCgU5BSBSnlHRlJ+hxC+9pexXkLgscXE2lCqHaqn4QwjeODLCRyN9mrm+GP/td0DDG3ktBZajulIQp9uP5d7ss/tSMMFFXSvyM19Rbo0pgAAmbJ5P7+XpRFJl5gYJ+WznQEBizEaSUVyQuvZ4lvlo2Fy0nhVdpuOFg4oy4aofA4Mv8Zm4zRmkieCq8oMIjRXrx/E6mVoWTwfFOCkmlkfDjPwdBU=


如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
上一篇:lua取参数
下一篇:lua mysql存日志

转载请注明:openresty中文及应用 出自老鄢博客 | 欢迎分享