Nginx + Lua 请求解析并转发请求

关键词:

  • nginx + lua 脚本
  • lua 获取 http 请求参数
  • lua + cjson 的使用
  • lua + resty.http 发送 http 请求
  • resty.http 与 socket.http

需求:
一个数据请求,携带数据标识(标识内含有平台信息)参数,去不同的平台获取对应的数据。数据存放再不同的平台上。

设计:
用 nginx 向外提供统一接口,在 nginx 内用 lua 脚本获取请求参数,根据参数中的平台信息将请求发送给不同的平台。

具体实现:
在 CentOS7 服务器上安装 nginx、lua、cjson、http 等库,然后进行各种测试(O.O)。

一、安装 Lua

在根目录下执行:

# 下载安装包
wget http://www.lua.org/ftp/lua-5.1.5.tar.gz

# 解压并进入文件
tar -xzvf lua-5.1.5.tar.gz
cd lua-5.1.5

# 安装依赖库
yum install -y readline-devel ncurses-devel

# 指定安装的系统平台
make linux

# 安装
make install

# 验证是否安装成功
lua

在这里插入图片描述

二、安装 Nginx

在根目录下执行:

# 下载安装包
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.9rc7.tar.gz
wget http://nginx.org/download/nginx-1.14.0.tar.gz 

tar -xvf v0.3.0.tar.gz
tar -xvf v0.10.9rc7.tar.gz
tar -xvf nginx-1.12.1.tar.gz

# 进入 nginx-1.14.0 文件夹并进行编译
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.9rc7  --with-http_ssl_module  --with-http_stub_status_module  --with-http_gzip_static_module
# --prefix: 安装目录(可自定义)
# --add-moudle = :相应模块的目录(可自定义)

# 安装
make install
	
# 安装后启动 nginx ,访问服务器 ip 是否安装成功

# 在 nginx.conf 文件中加入一下代码验证 nginx 是否可以访问到 lua
location /hello{
       default_type 'text/plain';
       content_by_lua 'ngx.say("Hello Word!")';
}

# 访问 http://ip/hello

Nginx + Lua 请求解析并转发请求_第1张图片

三、安装 cjson

在根目录下执行:

# 下载安装包
wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz

# 解压
tar -xvf lua-cjson-2.1.0.tar.gz
cd lua-cjson-2.1.0

# 安装
make install  
# 此模块主要用来解析 http body 部分,即 json.decode、 json.encode

四、安装 resty.http

在根目录下执行:

# 从 github 上下载文件
git clone https://github.com/ledgetech/lua-resty-http.git

# 将 lua-resty-http/lib/ 下的 resty 文件夹上传至服务器一下目录下
/usr/local/share/lua/5.1/

# 此模块主要用来发送 http 请求
# 也可选用 socket.http

五、编写 lua 脚本解析请求参数,并根据参数转发请求

# 脚本  /home/lua/test.lua
--引入库
local cjson = require 'cjson'
local https = require 'resty.http'
local json = cjson:new()
--服务器一
local server_one_url = 'http://ip:端口号/自定义'
local server_one_code = 'ONE_CODE';
--服务器二
local server_two_url = 'http://ip:端口号/自定义'
local server_two_code = 'TWO_CODE';

--http post 请求方法
local function http_post(url, data)
    local http = https:new()
    local res, err = http:request_uri(url, {
        method = 'POST',
        body = data,
        headers = {
            ['Content-Type'] = 'application/json';
        }
    })

    if res.status == 200 then
        ngx.say(res.body)
    else
        ngx.say('服务器错误!')
    end
end

--获取请求参数
ngx.req.read_body()
local data = ngx.req.get_body_data()
local object = json.decode(data)

--解析参数并做转发
local serverCode = object.serverCode
if serverCode ~= nil and serverCode == server_one_code then
    http_post(server_one_url, data)
elseif serverCode ~= nil and serverCode == server_two_code then
    http_post(server_two_url, data)
else
    ngx.say('未知服务!')
end

六、nginx 配置

# 在 nginx.conf 文件中加入一下配置
location /lua {
    content_by_lua_file   '/home/lua/test.lua';
}

七、测试

# url
http:ip:端口号/lua

# method
POST

# headers.Content-Type
application/json

# body
{
    "serverCode": "ONE_CODE"
}

TES and Lakers!
Pure And Desirous!

@

你可能感兴趣的:(linux,运维,linux,nginx,服务器)