需求:根据客户需求,可以在ngx下 通过lua做接口二次封装再次转发给用户或第三方

场景:对返回值有要求的、接口屏蔽字段、或做一些业务上的验证等

1、windows直接下载openresty 解压即可,就完成了windows下使用lua的开发环境

2、配置:

a、在nginx.conf里http下配置如下代码:

include       mime.types;
default_type application/octet-stream;
lua_package_path "/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/lualib/?.so;;"; #c模块
include lua.conf; #导入自定义lua配置文件
resolver 8.8.8.8;

b、在nginx.conf同目录创建lua.conf文件专门存放lua的路由配置

#lua.conf
server {
charset utf-8; #设置编码
listen 80;
server_name _;
location /user {
default_type 'text/html';
content_by_lua_file lua/api/userController.lua; #相对于nginx安装目录
}
}   

c、在ngx根目录下的lua文件夹里创建“api”文件夹,并且在里面添加userController.lua 处理文件类,例如代码如下:

local request_method = ngx.var.request_method
local args = nil --1、获取参数的值 获取前端提交参数
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end --2、组合url请求Get/Post请求 并获取参数
local http = require "resty.http"
local httpc = http.new()
local url = "http://xxxxx/user/login/"..args["userid"].."/"..args["pass"]
local resStr --响应结果
local res, err = httpc:request_uri(url, {
method = "GET",
--args = str,
body = "a=1&b=2",
headers = {
["Content-Type"] = "application/json",
}
}) --3、开始重新组合参数 例子 可根据返回的JSON自己处理
local cjson = require "cjson"
local sampleJson = [[{"age":"23","testArray":{"array":[8,9,11,14,25]},"Himi":"himigame.com"}]];
--解析json字符串
local data = cjson.decode(sampleJson);
--打印json字符串中的age字段
ngx.say(data["age"]);
--打印数组中的第一个值(lua默认是从0开始计数)
ngx.say(data["testArray"]["array"][1]); --4、打印输出新返回值
ngx.say(res.body)

3、启动ngx服务器,访问地址,路由为user  

windows下openresty中使用lua做接口转发、二次封装等的更多相关文章

  1. windows下apache + mod_wsgi + python部署flask接口服务

    windows下apache + mod_wsgi + python部署flask接口服务 用python3安装虚拟环境 为啥要装虚拟环境? 原因1:安装虚拟环境是为了使项目的环境和全局环境隔离开,在 ...

  2. windows下cmd中命令操作

    windows下cmd中命令:   cls清空 上下箭头进行命令历史命令切换 ------------------------------------------------------------- ...

  3. windows下VMware-workstation中安装CentOS

    windows下VMware-workstation中安装CentOS,可以分两部分,安装虚拟机和安装CentOS虚拟机.具体步骤如下: 一.安装虚拟机 1.安装VMware-workstation, ...

  4. (转)windows下VMware-workstation中安装CentOS

    windows下VMware-workstation中安装CentOS   windows下VMware-workstation中安装CentOS,可以分两部分,安装虚拟机和安装CentOS虚拟机.具 ...

  5. Windows下Git中正确显示中文的设置方法

    Windows下Git中正确显示中文的设置方法 具体设置方法如下: 进入目录etc:$ cd /etc 1. 编辑 gitconfig 文件:$ vi gitconfig.在其中增加如下内容: [gu ...

  6. openresty+lua做接口调用权限限制

    说明:openresty可以理解为一个服务器它将nginx的核心包含了过来,并结合lua脚本语言实现一些对性能要求高的功能,该篇文章介绍了使用openresty 1.purview.lua --调用j ...

  7. windows下caffe安装配置、matlab接口

    一.CommonSettings.props caffe下载后解压.源代码文件夹caffe-master,到该文件夹下的windows文件夹下,将CommonSettings.props.exampl ...

  8. Windows下Python中的中文路径和中文输出问题

    这几天有个项目需要写一点类似于脚本的小程序,就用Python写了,涉及到中文路径和中文输出的问题,整理一下. 有一个问题我觉得需要先强调一下,在写Python程序的时候,一定保证编码是utf-8,然后 ...

  9. Windows 下 Docker 的简单学习使用过程之二 Docker For windows

    1. Docker For windows 最新版也支持到了 docker ce 18.06 (这个博客的编写时间是 2018.8.17 当时是最新的) 2. 下载安装. 大概500m 左右的安装文件 ...

随机推荐

  1. Error creating bean with name 'com.cloud.feign.interfaces.xxxFeignClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalSt.PathVariable annotation was empty on

    环境: Spring Cloud:Finchley.M8 Spring Boot:2.0.0.RELEASE 报错信息: Error creating bean with name 'com.clou ...

  2. oracle学习篇七:更新操作、事务处理

    ----------------1.数据库更新操作----------------------------- select * from tab;--查询表 drop table siebel_use ...

  3. sql:无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲突。

    --无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲 ...

  4. HTML 5 表单相关元素和属性

    HTML使用表单向服务器提交请求,表单.表单控件的主要作用是收集用户输入,当用户提交表单时,用户输入内容将被作为请求参数提交到远程服务器.因此,在Web编程中,表单主要是用于收集用户输入的数据,在需要 ...

  5. sqlServer拼结列字符串

    with table1(sessionID,message,createTime)as(select 1 ,'hello' ,'2014/5/6' union allselect 1 ,'word' ...

  6. Windows平台字符串完全指南

    The Complete Guide to C++ Strings : The Complete Guide to C++ Strings, Part I - Win32 Character Enco ...

  7. Struts2的学习-通配符和session对象

    一. 取得session 3种方法1.context.getSession() -->>Map对象 2.HttpServletRequest request =(HttpServletRe ...

  8. 如何在SAP CRM里创建和消费Web service

    Created by Wang, Jerry, last modified on Dec 19, 2014 The following steps demonstrates how to expose ...

  9. MySQL学习(二)数据类型

    截取书中内容留作学习.... 1.整数类型 2.浮点数与定点数类型 3.日期时间类型 向数据库中插入当前系统时间:CURRENT_TIME或者NOW() 4.文本字符串类型 MySQL枚举类型:cre ...

  10. mxnet数据操作

    # coding: utf-8 # In[2]: from mxnet import nd # In[3]: x = nd.arange(12) x # In[4]: x.shape,x.size # ...