nginx-lua模块安装

一.简述

安装 Nginx 的 Lua 模块(通常指的是 ngx_lua 模块)可以显著增强 Nginx 的功能,使其能够执行 Lua 脚本。这使得 Nginx 可以处理更复杂的逻辑和动态内容生成。以下是详细的安装步骤,包括安装 OpenResty 或从源码编译 Nginx 和 ngx_lua 模块。

二.安装:

下载包:

# wget http://nginx.org/download/nginx-1.8.0.tar.gz

依赖
# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
# 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.5.tar.gz

luaJIT:利用JIT编译技术把Lua脚本直接编译成机器码由CPU运行

# tar fxvz LuaJIT-2.0.4.tar.gz 
# cd LuaJIT-2.0.4
# make && make install
# export LUAJIT_LIB=/usr/local/lib
# export LUAJIT_INC=/usr/local/include/luajit-2.0
# vim /etc/ld.so.conf.d/lua_luajit_lib.conf
/usr/local/lib
# ldconfig 

NDK(Nginx Development Kit)模块是一个拓展Nginx服务器核心功能的模块

第三方模块开发可以基于它来快速实现

NDK提供函数和宏处理一些基本任务,减轻第三方模块开发的代码量。

# tar fxvz v0.3.0.tar.gz 
# tar fxvz v0.10.5.tar.gz 

nginx 安装:

# ./configure --prefix=/vipkid/nginx_test --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E' --add-module=../ngx_devel_kit-0.3.0/ --add-module=../lua-nginx-module-0.10.5/

配置文件中加载路径:

lua_package_path "/etc/nginx/lua/?.lua;;";     #lua依赖库,多个用";"分隔
lua_package_cpath "/usr/lib/lua/5.1/?.so;;";   #c编写的so依赖库

三. 测试验证:

    location /lua {
        default_type 'text/html';      #默认的mime type
        content_by_lua_block {         #lua内容处理块,不应该和proxy_pass一起使用
            ngx.say("hello:",ngx.var.arg_a);   #打印内容, ngx.var.arg_a 为http的参数(a)
        }
    }

验证:
# curl http://10.136.27.175/lua?a=world
hello:world

lua_code_cache:

默认情况下开启,缓存lua代码,每次lua代码变更必须reload nginx才生效,如果在开发阶段可以通过lua_code_cache off关闭,这样调试时每次修改lua代码不需要reload nginx:

location /lua {
    default_type 'text/html';
    lua_code_cache off;  #在配置文件中的变动(如content_by_lua_block)不会生效, 生产环境应禁止,会带来风险和性能影响
    content_by_lua_file /etc/nginx/lua/test.lua;  #content_by_lua_file类似content_by_lua_block,不过内容单独写                                                         在了一个文件中,用于代码和配置隔离
    }
相关推荐
心随_风动34 分钟前
CentOS 下安装和配置 HTTPD 服务的详细指南
linux·运维·centos
信阳农夫39 分钟前
centos 7只能安装到3.6.8
linux·运维·centos
TT-Kun2 小时前
Linux | 进程控制(进程终止与进程等待)
linux·运维·服务器
...:...:...2 小时前
Linux 第三次脚本作业
linux·运维·服务器
web2u4 小时前
Docker入门及基本概念
java·运维·服务器·spring·docker·容器
MickeyCV4 小时前
Nginx学习笔记:常用命令&端口占用报错解决&Nginx核心配置文件解读
前端·nginx
元气满满的热码式5 小时前
Docker实战-使用docker compose搭建博客
运维·docker·容器
alenliu06215 小时前
跟着 Lua 5.1 官方参考文档学习 Lua (7)
lua
不良人天码星5 小时前
Linux的基础指令和环境部署,项目部署实战(下)
linux·运维·服务器
EasyNVR7 小时前
基于WebRTC与AI大模型接入EasyRTC:打造轻量级、高实时、强互动的嵌入式音视频解决方案
运维·服务器·微信·小程序·webrtc·p2p·智能硬件