Nginx與Lua開發

一、Lua及基礎語法

Nginx與Lua環境html

場景:用Nginx結合Lua實現代碼的灰度發佈nginx

一、Luagit

    是一個簡潔、輕量、可擴展的腳本語言github

二、Nginx+Lua優點web

    充分的結合Nginx的併發處理epoll優點和Lua的輕量併發

    實現簡單的功能切高併發的場景。dom

三、Lua的基礎語法ide

一、運行
[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 lewen

二、註釋

- 行註釋
--[[
塊註釋
--]]

三、變量
a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'

a = [[alo
    123]]
    布爾類型只有nil和false, false是數字0,' ' 空字符串('\0')都是true
    lua中的變量若是沒有特殊說明,全是全局變量

四、while循環語句
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
print(sum)

六、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 )

六、if-else判斷語句

"~="是不等於
字符串的拼接操做符".."
io庫的分別從stdin和stdout讀寫的read和write函數
View Code

教程 https://www.runoob.com/lua/lua-tutorial.html模塊化

 

二、Nginx+Lua環境部署

   參考https://blog.csdn.net/qq_38974634/article/details/81625075函數

一、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

二、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
分別解壓,等待被添加安裝

三、從新編譯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 輸出響應頭

相關文章
相關標籤/搜索