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", ""]

测试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

11: Nginx安装lua支持的更多相关文章

  1. Nginx安装lua支持

    Nginx安装lua支持 需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.下载安装LuaJIT-2.0.4.tar.gz wget -c ...

  2. nginx安装lua模块实现高并发

    nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...

  3. Nginx安装及支持https代理配置和禁用TSLv1.0、TSLv1.1配置

    Linux安装Nginx Nginx安装及支持https代理配置和禁用TSLv1.0.TSLv1.1配置. 下载安装包 [root@localhost ~]# wget http://nginx.or ...

  4. nginx安装部署(支持https)

    1      安装环境准备 1.1   准备环境清单 以下是基本环境清单列表: 软件名称 版本号 说明信息 Linux CentOS 6.7 部署机器只需为Linux系统即可,无严格要求 1.2   ...

  5. Let's Encrypt: 为CentOS/RHEL 7下的nginx安装https支持-具体案例

    环境说明: centos 7 nginx 1.10.2 前期准备 软件安装 yum install -y epel-release yum install -y certbot 创建目录及链接 方法1 ...

  6. nginx 安装第三方模块(lua)并热升级

    需求: nginx上将特定请求拒绝,并返回特定值. 解决办法: 使用lua脚本,实现效果. 操作步骤: 安装Luajit环境 重新编译nginx(目标机器上nginx -V 配置一致,并新增两个模块n ...

  7. Nginx安装以及配置

    安装编译工具及库文件 1 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 安装 PCRE 下载 PC ...

  8. 给lnmp一键包中的nginx安装openresty的lua扩展

    lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...

  9. Nginx安装配置|Nginx反向代理|Nginx支持HTTPS|Nginx重定向

    Nginx安装配置 可以直接看到最下面的HTTPS. Nginx安装 我的系统如下: No LSB modules are available. Distributor ID: Ubuntu Desc ...

随机推荐

  1. JS控制文本框只能输入数字 \保留小数点后两位

    <input type="text" placeholder="保留到小数点后两位" maxlength="200" onkeyup= ...

  2. 工作流引擎--swamp

    什么是工作流引擎(Workflow Engine )   例如开发一个系统最关键的部分不是系统的界面,也不是和数据库之间的信息交换,而是如何根据业务逻辑开发出符合实际需要的程序逻辑并确保其稳定性.易维 ...

  3. Amber TUTORIAL 4b: Using Antechamber to Create LEaP Input Files for Simulating Sustiva (efavirenz)-RT complex using the General Amber Force Field (GAFF)

    sustiva.pdb PDB: 1FKO Create parameter and coordinate files for Sustiva 1. 加氢: $ reduce sustiva.pdb ...

  4. 关于fullpage.js 和animate.css制作全屏简单大方的首页

    附上源码: html <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  5. 从零开始一起学习SLAM | 不推公式,如何真正理解对极约束?

    自从小白向师兄学习了李群李代数和相机成像模型的基本原理后,感觉书上的内容没那么难了,公式推导也能推得动了,感觉进步神速,不过最近小白在学习对极几何,貌似又遇到了麻烦... 小白:师兄,对极几何这块你觉 ...

  6. 转git的使用

    git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令) A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓 ...

  7. json_decode 转数组

    json_decode($json); 直接转义json数据后会发现转义后的数据时对象类型, 想要获得数组型,加一个参数 json_decode($json,true);

  8. Bootstrap-全局CSS样式-图片样式

    Bootstrap第二部分:全局CSS样式-图片样式.img-rounded   圆角图片.img-circle    圆形图片.img-thumbnail 缩略图片.img-responsive响应 ...

  9. hdu2609最小表示法

    #include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...

  10. uvalive 11865 Stream My Contest

    题意: 有一个网络中心,和许多个城市,网络中心以及城市之间有若干条边,这些边有两个属性,最大带宽和修建费用. 现在要用最多不超过C的费用修建网络,使得每个城市都有网络连接,最大化最小带宽. 带宽限制是 ...