Lua及基礎語法
Nginx與Lua環境
場景:用Nginx結合Lua實現代碼的灰度發布
1、Lua
是一個簡潔、輕量、可擴展的腳本語言
2、Nginx+Lua優勢
充分的結合Nginx的並發處理epoll優勢和Lua的輕量
實現簡單的功能切高並發的場景。
Lua的基礎語法
變量
-
a=[[alo123"]]
-
a=‘alo\n123"’
-
a=’\97lo\10\04923"’
布爾類型只有nil和false,數字0,空字符串(’ \0’)都是true
lua中的變量如果沒有特殊說明,全是全局變量
while循環語句 和 for循環語句
sum=0 num=1 while num<=100 do sum=sum+num num=num+1 end print("sum=",sum) lua沒有++或是+=這樣的操作 for循環 sum=0 for i=1 100 do sum=sum+i end
if-else判斷語句
if age==40 and sex=="male" then print("大於40男人") elseif age>40 and sex~="female" then print("大於60非女人") else local age=io.read() print("Your age is"..age) #字符串的拼接操作符 end
教程 https://www.runoob.com/lua/lua-tutorial.html
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.0
2、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 -y yum install zlib zlib-devel -y yum 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.9rc7 make && make install echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig 執行此 命令后,nginx -V 看下參數
測試Lua
# cd /etc/nginx/conf.d/default.conf 加入下面的location server { 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)'; } }
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 輸出響應頭
灰度發布
- 按照一定的關系區別,分部分的代碼進行上線,使代碼的發布能平滑過渡上線。
- 用戶的信息cookie等信息區別
- 根據用戶的ip地址
dep.conf 配置
server { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/nginx/log/host.access.log main; location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } location /myip { default_type 'text/plain'; content_by_lua ' clientIP = ngx.req.get_headers()["x_forwarded_for"] ngx.say("IP:",clientIP) '; } location / { default_type "text/html"; content_by_lua_file /opt/app/lua/dep.lua; #add_after_body "$http_x_forwarded_for"; } location @server{ proxy_pass http://127.0.0.1:9090; } location @server_test{ proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
dep.lua配置
clientIP = ngx.req.get_headers()["X-Real-IP"] if clientIP == nil then clientIP = ngx.req.get_headers()["x_forwarded_for"] end if clientIP == nil then clientIP = ngx.var.remote_addr end local memcached = require "resty.memcached" local memc, err = memcached:new() if not memc then ngx.say("failed to instantiate memc: ", err) return end local ok, err = memc:connect("127.0.0.1", 11211) if not ok then ngx.say("failed to connect: ", err) return end local res, flags, err = memc:get(clientIP) ngx.say("value key: ",res,clientIP) if err then ngx.say("failed to get clientIP ", err) return end if res == "1" then ngx.exec("@server_test") return end ngx.exec("@server")