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环境部署

参考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 看下参数

3、测试Lua

# cd /etc/nginx/conf.d/default.conf 加入下面的location

server {
listen ;
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 输出响应头

Nginx与Lua开发的更多相关文章

  1. nginx 与 lua 开发环境搭建

    首先下载最新版的 相关软件 的安装文件. nginx: http://nginx.org/en/download.html LuaJIT: http://luajit.org/download.htm ...

  2. nginx 与 lua 开发笔记

    Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...

  3. Nginx与Lua的开发

    1. Lua基础语法 安装lua hello world 也可以编写lua脚本 运行脚本 lua注释 变量 局部变量的话前面加个local 循环 if语句 2. Nginx与Lua开发环境 https ...

  4. Nginx详解二十三:Nginx深度学习篇之Nginx+Lua开发环境搭建

    Nginx+Lua开发环境 1.下载LuaJIT解释器wget http://luajit.org/download/LuaJIT-2.0.2.tar.gztar -zxvf LuaJIT-2.0.2 ...

  5. (转)OpenResty(nginx+lua) 开发入门

    原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/  Open ...

  6. CentOS安装OpenResty(Nginx+Lua)开发环境

    一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...

  7. 单机闭环 使用Nginx+Lua开发高性能Web应用

    [西域骆驼D1532101213]西域骆驼(VANCAMEL)D1532101213 休闲套脚鞋 卡其43[行情 报价 价格 评测]-京东 http://item.jd.com/1856564.htm ...

  8. OpenResty(Nginx+Lua)开发入门

    Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...

  9. 跟我学OpenResty(Nginx+Lua)开发目录贴 (转)

    使用Nginx+Lua开发近一年的时间,学习和实践了一些Nginx+Lua开发的架构,为了让更多人使用Nginx+Lua架构开发,利用春节期间总结了一份基本的学习教程,希望对大家有用.也欢迎谈探讨学习 ...

随机推荐

  1. seanborn使用函数regplot回归分析绘图

    可以用regplot(x, y, data)绘制回归图.data参数是DataFram类型,x是其中某一列列名,是即将绘制的图的x坐标,y是其中某一列,是图的y坐标 下面代码是对seaborn内置数据 ...

  2. 记一次有趣的JsonFormat不生效问题

    dto中使用了JsonFormat注解,如图 然后再序列化时 objectMapper.writeValueAsString(printReceBillVO) 始终值是一个Long,最后发现是包引用错 ...

  3. cqoj921E整数匹配

    这是一个贪心题,把我坑的好惨,忘还原得70.上午被卡得,, 首先给出长度为n的一组数,可以两两配对相乘也可以进行相加,问怎样才可以使总和最大?那么可以显然看出来,当这个数为0或1时,我们要相加.其余进 ...

  4. Tabcontrol动态添加TabPage(获取或设置当前选项卡及其属性)

    http://blog.csdn.net/xiongxyt2/article/details/6920575 •MultiLine 属性用true 或false来确定是否可以多行显示 •Appeara ...

  5. UEditor使用报错Cannot set property 'innerHTML' of undefined

    仿用UEditor的setContent的时候报错,报错代码如下Uncaught TypeError: Cannot set property ‘innerHTML’ of undefined.调试u ...

  6. 新建maven子模块 出现 Unable to read parent POM

    新建maven子模块 出现 Unable to read parent POM错误 于是把pom.xml文件中的 中文字符全部删除 包括 注释 最后成功建立

  7. __bridge

    Core Foundation 框架Core Foundation框架 (CoreFoundation.framework) 是一组C语言接口,它们为iOS应用程序提供基本数据管理和服务功能.下面列举 ...

  8. ELK + filebeat集群部署

    ELK + filebeat集群部署 一.ELK简介 1. Elasticsearch Elasticsearch是一个实时的分布式搜索分析引擎, 它能让你以一个之前从未有过的速度和规模,去探索你的数 ...

  9. Linux架构之Nginx 高可用

    第53章 Nginx之高可用Keepalived 一.Keepalived高可用基本概述 1.1)什么是高可用 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快 ...

  10. VB.NET Event RaiseEvent用处

    一.代码 Private Sub Form1_Load(ByVal sender As Object, _                       ByVal e As System.EventA ...