在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
,請點擊這裏查看。