Nginx支持lua脚本

使用场景

使用lua嵌入到Nginx当中必须要让Nginx支持lua,使用lua模块进行html的模板渲染,速度比php的模板引擎渲染;

一.安装lua解释器

[root@VM_0_15_centos ~]# cd /usr/local/src
#从lua官网上下载
[root@VM_0_15_centos src]# wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
[root@VM_0_15_centos src]# tar xzvf LuaJIT-2.0.4.tar.gz
[root@VM_0_15_centos src]# cd LuaJIT-2.0.4
#安装并且指定安装路径
[root@VM_0_15_centos LuaJIT-2.0.4]# make install PREFIX=/usr/local/luajit
#安装完成后添加到环境变量
[root@VM_0_15_centos LuaJIT-2.0.4]# export LUAJIT_LIB=/usr/local/luajit/lib
[root@VM_0_15_centos LuaJIT-2.0.4]# export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
#能查看到版本号说明安装成功
[root@VM_0_15_centos LuaJIT-2.0.4]# lua -v                                                                                   
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

二.下载解压相关模块

#下载解药ngx_devel_kit模块
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xzvf v0.3.0.tar.gz
#设置环境变量
NGINX_NGX=#PWD/ngx_devel_kit-0.3.0
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
tar -xzvf v0.10.13.tar.gz
#设置环境变量
NGINX_NGX=#PWD/lua-nginx-module-0.10.13

三.安装Nginx的lua模块

重新编译 请看如何在已经安装nginx上新增模块
#在原配置上后面添加 通过nginx -V查看
--add-module=$NGINX_LUA --add-module=$NGINX_NGX 
--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" #lua编译器安装目录

注意

Nginx新增模块后需要重启Nginx新模块才会生效


测试下

server {                                                                                                                 
        listen       7000;                                                                                                                                                                                         
        server_name  localhost;                                                                                              
        location / {                                                                                                         
          default_type text/html;                                                                                            
          content_by_lua 'ngx.say("1212222")';                                                                               
        }                                                                                                                    
        location /test {                                                                                                     
          default_type text/html;                                                                                            
          content_by_lua_file "/www/test.lua";                                                                                                                                                                                                  
        }                                                                                                                            
    }
#在/www/test.lua文件中添加ngx.say("hello world");

MyAnswer博客


MyAnswer博客

MyAnswer博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论