Nginx与Lua开发
1、Lua及基礎語法
Nginx與Lua環境
場景:用Nginx結合Lua實現代碼的灰度發布
1、Lua
??? 是一個簡潔、輕量、可擴展的腳本語言
2、Nginx+Lua優勢
??? 充分的結合Nginx的并發處理epoll優勢和Lua的輕量
??? 實現簡單的功能切高并發的場景。
3、Lua的基礎語法
1、運行 [root@web-01 ~]# lua Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio > print("Hi I'm lewen") Hi I'm lewen[root@web-01 ~]# lua ./lewen.lua Hi I'm lewen2、注釋- 行注釋 --[[ 塊注釋 --]]3、變量 a = 'alo\n123"' a = "alo\n123\"" a = '\97lo\10\04923"'a = [[alo123]]布爾類型只有nil和false, false是數字0,' ' 空字符串('\0')都是truelua中的變量如果沒有特殊說明,全是全局變量4、while循環語句 sum = 0 num = 1 while num <= 100 dosum = sum + numnum = num + 1 end print("sum=",sum)Lua沒有 ++ 或是 += 這樣的操作5、for循環語句 sum=0 for i=1,100 do sum=sum+i end print(sum)6、if-else判斷語句a = 100 --[ 檢查布爾條件 --] if( a == 10 ) then--[ 如果條件為 true 打印以下信息 --]print("a 的值為 10" ) elseif( a == 20 ) then--[ if else if 條件為 true 時打印以下信息 --]print("a 的值為 20" ) elseif( a == 30 ) then--[ if else if condition 條件為 true 時打印以下信息 --]print("a 的值為 30" ) else--[ 以上條件語句沒有一個為 true 時打印以下信息 --]print("沒有匹配 a 的值" ) end print("a 的真實值為: ", a )6、if-else判斷語句"~="是不等于 字符串的拼接操作符".." io庫的分別從stdin和stdout讀寫的read和write函數 View Code教程 https://www.runoob.com/lua/lua-tutorial.html
2、Nginx+Lua環境部署
?? 參考https://blog.csdn.net/qq_38974634/article/details/81625075
1、LuaJIT
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/etc/profile 文件中加入環境變量 export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.02、ngx_devel_kit和lua-nginx-module
cd /opt/download wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz 分別解壓,等待被添加安裝3、重新編譯Nginx
下載nginx的源碼包cd /data wget http://nginx.org/download/nginx-1.12.1.tar.gz tar -zxvf nginx-1.12.1.tar.gz;cd nginx-1.12.1安裝依賴yum install openssl openssl-devel -yyum install zlib zlib-devel -yyum install pcre-devel –y編譯./configure --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=/data/ngx_devel_kit-0.3.0 --add-module=/data/lua-nginx-module-0.10.9rc7make && make installecho "/usr/local/lib" >> /etc/ld.so.confldconfig 執行此 命令后,nginx -V 看下參數3、測試Lua
# cd /etc/nginx/conf.d/default.conf 加入下面的locationserver {listen 80;server_name web01.fadewalk.com;location /lua {set $test "hello,world";content_by_lua 'ngx.header.content_type="text/plain"ngx.say(ngx.var.test)';}}?
4、Nginx調用lua模塊指令
Nginx的可插拔模塊化加載執行,共11個處理階段
set_by_lua??????? 設置nginx變量,可以實現復雜的賦值邏輯
set_by_lua_file
access_by_lua???? 請求訪問階段處理,用于訪問控制
access_by_lua_file
content_by_lua??? 內容處理器,接收請求處理并輸出響應
content_by_lua_file
ngx.var???? nginx變量
ngx.req.get headers 獲取請求頭
ngx.req.get_uri_args 獲取url請求參數
ngx.redirect??? 重定向
ngx.print??? 輸出響應內容體
ngx.say 通ngx.print,但是會最后輸出一個換行符
ngx.header 輸出響應頭
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Nginx与Lua开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Nginx的https服务
- 下一篇: nginx之Geoip读取地域信息模块