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,不过内容单独写                                                         在了一个文件中,用于代码和配置隔离
    }
相关推荐
Kookoos21 分钟前
Dynamics 365 Finance + Power Automate 自动化凭证审核
运维·自动化·dynamics 365·power automate
努力学习的小廉4 小时前
深入了解linux系统—— 进程池
linux·运维·服务器
秃头菜狗4 小时前
各个主要目录的功能 / Linux 常见指令
linux·运维·服务器
jiunian_cn5 小时前
【Linux】centos软件安装
linux·运维·centos
藥瓿亭6 小时前
K8S认证|CKS题库+答案| 6. 创建 Secret
运维·ubuntu·docker·云原生·容器·kubernetes·cks
2302_809798326 小时前
【JavaWeb】Docker项目部署
java·运维·后端·青少年编程·docker·容器
嵌入式大圣6 小时前
Neko虚拟浏览器远程协作方案:Docker+内网穿透技术部署实践
运维·docker·容器
dmy6 小时前
n8n内网快速部署
运维·人工智能·程序员
程序员JerrySUN6 小时前
全面理解 Linux 内核性能问题:分类、实战与调优策略
java·linux·运维·服务器·单片机
米粉03057 小时前
深入剖析Nginx:从入门到高并发架构实战
java·运维·nginx·架构