01: Nginx安装lua支持

1.1 Nginx 使用lua脚本

  注:需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module

  1、Nginx安装lua支持

      wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz

      tar xzvf LuaJIT-2.0.4.tar.gz

      cd LuaJIT-2.0.4

      make install PREFIX=/usr/local/luajit

      # 注意环境变量!

      export LUAJIT_LIB=/usr/local/luajit/lib

      export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

  2、下载解压ngx_devel_kit

      wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

      tar -xzvf v0.3.0.tar.gz

  3、下载解压lua-nginx-module

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

      tar -xzvf v0.10.8.tar.gz

  4、下载安装nginx-1.10.3.tar.gz

      wget http://nginx.org/download/nginx-1.10.3.tar.gz

      tar -xzvf nginx-1.10.3.tar.gz

      cd nginx-1.10.3

      ./configure --add-module=/opt/soft/ngx_devel_kit-0.3.0 --add-module=/opt/soft/lua-nginx-module-0.10.8

      # 注1:ngx_devel_kit和lua-nginx-module以实际解压路径为准

      make -j2

      make install

      # 注2:报错gcc需要安装,可以执行

      yum install -y gcc g++ gcc-c++

      依赖报错,可以执行

      yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

  5、启动nginx

      ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

        /usr/local/nginx/sbin/nginx   # 启动nginx

      /usr/local/nginx/sbin/nginx -s stop     # 关闭nginx

      注:创建nginx启动脚本: /etc/init.d/nginx

#!/bin/bash
#chkconfig: 2345 89 89
#Description:This is Nginx web script"
PID="/usr/local/nginx/logs/nginx.pid"
start(){
        /usr/local/nginx/sbin/nginx
        if [ $? -eq 0 ];then
                echo -en "Starting Nginx...\t\t\t["
                echo -en "\033[32;34mOK\033[0m"
                echo "]"
        else
                echo "Starting Nginx Error"
        fi
}
stop(){
        /usr/local/nginx/sbin/nginx -s stop
        if [ $? -eq 0 ];then
                echo -en "Stop Nginx...\t\t\t["
                echo -en "\033[32;34mOK\033[0m"
                echo "]"
        else
                echo "Stop Nginx Error"
        fi
}
status(){
        if [ -f $PID ];then
                ID=$(cat $PID)
                echo "Ngix($ID) is running..."
        else
                echo "Nginx is stop"
        fi
}
case $1 in
start)
        start;;
stop)
        stop;;
restart)
        stop
        start
        ;;
status)
        status;;
*)
        echo "Usage:$0 {start|stop|restart|status}"
esac
/etc/init.d/nginx

  6、验证

      vim /usr/local/nginx/conf/nginx.conf   # 配置nginx.conf

      #test.lua文件内容

      ngx.say("hello world");

#1、lua指令方式
#在server 中添加一个localtion
location /hello {
            default_type 'text/plain';
            content_by_lua 'ngx.say("hello, lua")';
}

#2、lua文件方式
#在server 中添加一个localtion
location /lua {
    default_type 'text/html';
    content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
nginx.conf

      [root@redis nginx]# curl http://127.0.0.1/hello
      hello, lua
      [root@redis nginx]# curl http://127.0.0.1/lua
      hello world

1.2 nginx 配置lua脚本 

  1、lua配置get请求

         location /api/test {
                # access_log logs/access_api.log  main;
                set $cmd "python  /home/work/opbin/test.py $arg_username $arg_fullname $arg_email $arg_phone 2>&1";
                content_by_lua '
                        ngx.header.content_type = "text/plain";
                        local cmd = ngx.var.cmd;
                        local exec = io.popen(cmd);
                        local log = exec:read("*all");
                        exec:close();
                        ngx.print(log);
                ';
        }
nginx.conf 配置lua接收get请求
#1、在浏览器中访问
http://1.1.1.3/api/test?username=jack4&fullname=JACK4&email=jack4@yiducloud.cn&phone=18765452444

#2、返回结果
["/home/work/opbin/test.py", "jack4", "JACK4", "jack4@yiducloud.cn", "18765452444"]
测试lua接收get请求
local secondfile = io.popen("ifconfig")
if nil == secondfile then
    print("open file for ipconfig fail")
end

print("\n======commond ipconfig result:")

local content = secondfile:read("*a")
print(content)

secondfile:close()

# lua test.lua 
test.lua 执行系统命令

  2、lua配置post请求

 

 

 

 

 

 

 

 

1111111111111111111111

posted @ 2018-12-18 10:18  不做大哥好多年  阅读(459)  评论(0编辑  收藏  举报