[深] nginx與lua的開發(fā)(28)

獲取全套nginx教程横媚,請訪問瓦力博客

lua是一個簡潔纠炮、輕量月趟、可擴展的腳本語言灯蝴。nginx+lua開發(fā)充分的結(jié)合Nginx的并發(fā)處理epoll優(yōu)勢和lua的輕量實現(xiàn)簡單的功能切高并發(fā)的場景。

1.安裝lua

如果是centos7版本之上會自帶lua孝宗,版本5.1.4穷躁。lua沒有安裝

yum install lua

2.安裝LuaJIT

安裝LuaJIT環(huán)境,比自帶的lua解析器擴展模塊多并且高效。

cd /opt/download
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -zxvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make install  PREFIX=/usr/local/LuaJIT
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

加載lua庫问潭,加入到ld.so.conf文件

echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
ldconfig

3.下載ngx_devel_kit和lua-nginx-module

cd /opt/download
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -zxvf v0.10.9rc7.tar.gz

4.nginx編譯模塊

將ngx_devel_kit模塊和lua-nginx-module模塊編譯進nginx

cd /opt/download/nginx-1.14.2
nginx -V

輸出

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/echo-nginx-module-0.61 --with-http_geoip_module

添加ngx_devel_kit模塊和lua-nginx-module模塊添加到編譯參數(shù)

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/echo-nginx-module-0.61 --with-http_geoip_module --add-module=/opt/download/lua-nginx-module-0.10.9rc7 --add-module=/opt/download/ngx_devel_kit-0.3.0

./configure 編譯參數(shù)
make && make install

5.nginx調(diào)用lua模塊指令

nginx的可插拔模塊化加載執(zhí)行猿诸,共11個處理階段

參數(shù) 描述
set_by_lua
set_by_lua_file
設(shè)置nginx變量,可以實現(xiàn)復雜的復雜邏輯
access_by_lua
access_by_lua_file
請求訪問階段處理狡忙,用于訪問控制
content_by_lua
content_by_lua_file
內(nèi)容處理容器梳虽,介紹請求處理并輸出響應

6.nginx調(diào)用lua的api

api 描述
ngx.var nginx變量
ngx.req.get_headers 獲取請求頭
ngx.req.get_uri_args 獲取url請求參數(shù)
ngx.redirect 重定向
ngx.print 輸出響應內(nèi)容體
ngx.say 通ngx.print,但是最后會輸出一個換行符
ngx.header 輸出響應頭
...

7.灰度發(fā)布

按照一定的關(guān)系區(qū)別,分部分的代碼進行上線灾茁,使代碼的發(fā)布能平滑過渡上線窜觉。

未完成待續(xù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市北专,隨后出現(xiàn)的幾起案子禀挫,更是在濱河造成了極大的恐慌,老刑警劉巖拓颓,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件语婴,死亡現(xiàn)場離奇詭異,居然都是意外死亡驶睦,警方通過查閱死者的電腦和手機砰左,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來啥繁,“玉大人菜职,你說我怎么就攤上這事∑烀觯” “怎么了酬核?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長适室。 經(jīng)常有香客問我嫡意,道長,這世上最難降的妖魔是什么捣辆? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任蔬螟,我火速辦了婚禮,結(jié)果婚禮上汽畴,老公的妹妹穿的比我還像新娘旧巾。我一直安慰自己,他們只是感情好忍些,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布鲁猩。 她就那樣靜靜地躺著,像睡著了一般罢坝。 火紅的嫁衣襯著肌膚如雪廓握。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天,我揣著相機與錄音隙券,去河邊找鬼男应。 笑死,一個胖子當著我的面吹牛娱仔,可吹牛的內(nèi)容都是我干的沐飘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼牲迫,長吁一口氣:“原來是場噩夢啊……” “哼薪铜!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起恩溅,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤隔箍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后脚乡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蜒滩,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年奶稠,在試婚紗的時候發(fā)現(xiàn)自己被綠了俯艰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡锌订,死狀恐怖竹握,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情辆飘,我是刑警寧澤啦辐,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站蜈项,受9級特大地震影響芹关,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜紧卒,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一侥衬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧跑芳,春花似錦轴总、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至坡倔,卻和暖如春漂佩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背罪塔。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工投蝉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人征堪。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓瘩缆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親佃蚜。 傳聞我的和親對象是個殘疾皇子庸娱,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容