Nginx与Lua开发

OpenResty —— 通过 Lua 扩展 NGINX 实现的可伸缩的 Web 平台。: https://openresty.org/cn/commercial.html

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 lewen

2、注释

- 行注释
--[[
块注释
--]]

3、变量
a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'

a = [[alo
    123]]
    布尔类型只有nil和false, false是数字0,' ' 空字符串('\0')都是true
    lua中的变量如果没有特殊说明,全是全局变量

4、while循环语句
sum = 0
num = 1
while num <= 100 do
  sum = sum + num
  num = 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函数

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

2、Nginx+Lua环境部署

 

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

这里要注意有坑,设置环境变量完成后要source ..... 一下,下面的lib 与inc需要写正确的路径,则否会出错。
/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 看下参数

3、测试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)';

    }

}

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 输出响应头

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值