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 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开发的更多相关文章
- nginx 与 lua 开发环境搭建
首先下载最新版的 相关软件 的安装文件. nginx: http://nginx.org/en/download.html LuaJIT: http://luajit.org/download.htm ...
- nginx 与 lua 开发笔记
Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...
- Nginx与Lua的开发
1. Lua基础语法 安装lua hello world 也可以编写lua脚本 运行脚本 lua注释 变量 局部变量的话前面加个local 循环 if语句 2. Nginx与Lua开发环境 https ...
- Nginx详解二十三:Nginx深度学习篇之Nginx+Lua开发环境搭建
Nginx+Lua开发环境 1.下载LuaJIT解释器wget http://luajit.org/download/LuaJIT-2.0.2.tar.gztar -zxvf LuaJIT-2.0.2 ...
- (转)OpenResty(nginx+lua) 开发入门
原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/ Open ...
- CentOS安装OpenResty(Nginx+Lua)开发环境
一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...
- 单机闭环 使用Nginx+Lua开发高性能Web应用
[西域骆驼D1532101213]西域骆驼(VANCAMEL)D1532101213 休闲套脚鞋 卡其43[行情 报价 价格 评测]-京东 http://item.jd.com/1856564.htm ...
- OpenResty(Nginx+Lua)开发入门
Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...
- 跟我学OpenResty(Nginx+Lua)开发目录贴 (转)
使用Nginx+Lua开发近一年的时间,学习和实践了一些Nginx+Lua开发的架构,为了让更多人使用Nginx+Lua架构开发,利用春节期间总结了一份基本的学习教程,希望对大家有用.也欢迎谈探讨学习 ...
随机推荐
- Ubuntu新建用户以及安装pytorch
环境:Ubuntu18,Python3.6 首先登录服务器 ssh username@xx.xx.xx.xxx #登录一个已有的username 新建用户 sudo adduser username ...
- 应用安全 - 无文件攻击 - Office漏洞 - 汇总
CVE-2017-0199 Date: -1 类型: 弹窗|内网穿透导致远程代码执行 影响范围: Microsoft Office 2007 Service Pack 3 Microsoft Offi ...
- Cassandra视图
一.简介 Cassandra作为一个P2P结构的NOSQL数据库,使用与HBase不同的去中心化架构,在国外使用非常广泛,受欢迎程度甚至在Hbase之上.今天这篇文章介绍Cassandra在视图方面设 ...
- [官网]Prevent a worm by updating Remote Desktop Services (CVE-2019-0708)
Prevent a worm by updating Remote Desktop Services (CVE-2019-0708) ★★★★★ https://blogs.technet.micro ...
- CentOS 7安装Python 2.6(与已有版本共存)
1. 安装需要用到的包 yum install -y zlib-devel bzip2-devel openssl-devel xz-libs wget 2. 下载 Python 2.6.8 版本 w ...
- Eureka 源码分析之 Eureka Client
文章首发于微信公众号<程序员果果> 地址:https://mp.weixin.qq.com/s/47TUd96NMz67_PCDyvyInQ 简介 Eureka是一种基于REST(Repr ...
- Debian/Ubuntu下安装Apache的Mod_Rewrite模块的步骤分享
启用 Mod_rewrite 模块:sudo a2enmod rewrite 另外,也可以通过将 /etc/apache2/mods-available/rewrite.load 连接到 /etc/a ...
- python中输入某年某月某日,判断这一天是这一年的第几天?
输入某年某月某日,判断这一天是这一年的第几天?程序分析 特殊情况,闰年时需考虑二月多加一天: 直接上代码 #定义一个函数,判断是否为闰年 def leapyear(y): return (y % 40 ...
- UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)
秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- loli的测试-2018.12.9
模拟赛-2018.12.9 这是NOIP之后第一次模拟赛...但是考的比较悲惨. 非常喜欢写考试总结,不知道为什么... T1:https://www.luogu.org/problemnew/sho ...