lua是一个简洁、轻量、可扩展的脚本语言。nginx+lua开发充分的结合Nginx的并发处理epoll优势和lua的轻量实现简单的功能切高并发的场景。

1.安装lua

如果是centos7版本之上会自带lua,版本5.1.4。lua没有安装

yum install lua

2.安装LuaJIT

安装LuaJIT环境,比自带的lua解析器扩展模块多并且高效。

cd /opt/download
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
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

加载lua库,加入到ld.so.conf文件

echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
ldconfig

3.下载ngx_devel_kit和lua-nginx-module

cd /opt/download
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -zxvf v0.10.9rc7.tar.gz

4.nginx编译模块

将ngx_devel_kit模块和lua-nginx-module模块编译进nginx

cd /opt/download/nginx-1.14.2
nginx -V

输出

--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=/opt/download/echo-nginx-module-0.61 --with-http_geoip_module

添加ngx_devel_kit模块和lua-nginx-module模块添加到编译参数

--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=/opt/download/echo-nginx-module-0.61 --with-http_geoip_module --add-module=/opt/download/lua-nginx-module-0.10.9rc7 --add-module=/opt/download/ngx_devel_kit-0.3.0

./configure 编译参数
make && make install

5.nginx调用lua模块指令

nginx的可插拔模块化加载执行,共11个处理阶段

参数描述
set_by_lua
set_by_lua_file
设置nginx变量,可以实现复杂的复杂逻辑
access_by_lua
access_by_lua_file
请求访问阶段处理,用于访问控制
content_by_lua
content_by_lua_file
内容处理容器,介绍请求处理并输出响应

6.nginx调用lua的api

api描述
ngx.varnginx变量
ngx.req.get_headers获取请求头
ngx.req.get_uri_args获取url请求参数
ngx.redirect重定向
ngx.print输出响应内容体
ngx.say通ngx.print,但是最后会输出一个换行符
ngx.header输出响应头
 

7.灰度发布

按照一定的关系区别,分部分的代码进行上线,使代码的发布能平滑过渡上线。

未完成待续

nginx教程

nginx环境搭建(1) nginx基础知识(2) nginx的安装 卸载(3) nginx的基本参数使用(4) nginx分析默认配置(5) nginx 虚拟主机配置(6) nginx 日志(7) nginx 模块(8) nginx 访问控制(9) nginx 静态资源web服务(10) nginx 缓存(11) nginx 跨域访问(12) nginx 防盗链(13) nginx 正向,反向代理配置(14) nginx 代理缓存配置(15) nginx websocket(16) nginx fastcgi(17) nginx 搭建wordPress博客(18) nginx Fastcgi缓存配置(19) nginx uwsgi反向代理(20) nginx 负载均衡(21) [深] nginx 动静分离(22) [深] nginx rewrite规则(23) [深] nginx 平滑升级 添加模块 调试(24) [深] nginx secure_link_module模块(25) [深] nginx geoip_module模块(26) [深] nginx https(27) [深] nginx与lua的开发(28) [架] nginx常见问题(29) [架] nginx性能优化(30) [架] nginx 安全(31) [架] nginx 反向代理gRpc(32)