在2016年第二届 OpenResty
的全球开发者大会上看到了一个比较有意思的项目 lua-resty-repl,后来听闻一些开发者看了项目的介绍后仍是以为一头雾水,不知道怎么使用。这篇文章主要是介绍一下这个项目的使用方法。html
根据做者介绍这是一个简单和容易调试运行在 OpenResty
的 lua
。前端
根据 官网 介绍,快速开始的姿式以下:nginx
$ wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz $ tar zxpf luarocks-2.4.1.tar.gz $ cd luarocks-2.4.1 $ ./configure; sudo make bootstrap $ sudo luarocks install luasocket $ lua Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio > require "socket"
若是遇到系统上缺乏 lualib.h
依赖致使没法正常的经过的,请到 Lua 官网 上先下载好最新的 Lua
源码编译安装解决,或指定 Lua
源码中lualib.h
的目录便可。相信安装 luarocks
过程当中各类失败问题你们很容易能解决,这里就不详细展开。git
luarocks install lua-resty-repl
先建立一个 nginx.conf
文件并向文件中写入配置信息:github
master_process off; # 关闭 master 进程 error_log stderr notice; # error_log 日志级别是 stderr notice daemon off; # 关闭守护进程模式,即打开调试模式 events { worker_connections 1024; } http { server { listen 8080; lua_code_cache off; location / { content_by_lua_block { require('resty.repl').start() } } } }
将上面的 nginx.conf
替换掉 OpenResty
安装后 conf
文件夹中的 nginx.conf
后,启动 OpenResty
:web
$nginx Time [alert] #0: lua_code_cache is off; this will hurt performance in nginx.conf nginx: [alert] lua_code_cache is off; this will hurt performance in nginx.conf Time [notice] #0: using the "epoll" event method Time [notice] #0: openresty/1.11.2.1 Time [notice] #0: built by gcc 4.9.2 (Debian 4.9.2-10) Time [notice] #0: OS: Linux 4.4.0-38-generic Time [notice] #0: getrlimit(RLIMIT_NOFILE): 65536:65536
能够看到打出一些调试信息,如今咱们启动另一个终端,或者使用 tmux 分屏,在另外一个屏上输入:shell
curl -H X-Header:buz 172.17.0.2:8080?foo=bar
那么你将会在原来的调试信息上看到以下输出内容:bootstrap
Time [alert] 639#0: lua_code_cache is off; this will hurt performance in nginx.conf nginx: [alert] lua_code_cache is off; this will hurt performance in nginx.conf Time [notice] 639#0: using the "epoll" event method Time [notice] 639#0: openresty/1.11.2.1 Time [notice] 639#0: built by gcc 4.9.2 (Debian 4.9.2-10) Time [notice] 639#0: OS: Linux 4.4.0-38-generic Time [notice] 639#0: getrlimit(RLIMIT_NOFILE): 65536:65536 [1] ngx(content)>
在 >
后面写入 ngx.req.get_headers()
和 ngx.req.get_uri_args()
之类的命令后能够看到:框架
Time [alert] 639#0: lua_code_cache is off; this will hurt performance in nginx.conf nginx: [alert] lua_code_cache is off; this will hurt performance in nginx.conf Time [notice] 639#0: using the "epoll" event method Time [notice] 639#0: openresty/1.11.2.1 Time [notice] 639#0: built by gcc 4.9.2 (Debian 4.9.2-10) Time [notice] 639#0: OS: Linux 4.4.0-38-generic Time [notice] 639#0: getrlimit(RLIMIT_NOFILE): 65536:65536 [1] ngx(content)> ngx.req.get_headers() => { accept = "*/*", host = "172.17.0.2:8080", ["user-agent"] = "curl/7.47.0", ["x-header"] = "buz", <metatable> = { __index = <function 1> } } [2] ngx(content)> ngx.req.get_uri_args() => { foo = "bar" } [3] ngx(content)> ngx.say 'it works!' => 1 [4] ngx(content)> ngx.exit(ngx.OK) 172.17.0.1 - - [Time +0000] "GET /?foo=bar HTTP/1.1" 200 20 "-" "curl/7.47.0"
从上面能够看出,当一个请求过来的时候,开始执行咱们手动输入的代码,直到遇到 ngx.exit()
才返回结果给客户端。curl
顺带一提,此次看完在腾讯举办的 第二届 OpenResty
的全球开发者大会 后以为很赞,干货满满的,还有 OpenResty
创始人 agentzh
和几位牛逼的讲师在大会上出色演讲和精彩答疑,确实让你们收获很大。
简单介绍一下此次大会,此次 OpenResty
大会的主题是 web
开发,涉及到 OpenResty
在前端系统、API gateway
、 web
框架、集群服务、语音云服务、智能硬件等方面的实践,以及 OpenResty
软件基金会背后的故事。想要回顾大会优质的视频回放和 PPT
,请点击这里查看。