window openresty 执行http请求,windowopenresty
window openresty 执行http请求,windowopenresty
下载地址 https://github.com/LomoX-Offical/nginx-openresty-windows
直接解压即可
1 在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;
在nginx.conf同目录创建lua.conf文件专门存放lua的路由配置
#lua.conf
server {
charset utf-8; #设置编码
listen 8080;
server_name _;
location /test {
default_type 'text/html';
content_by_lua_file lua/api/testController.lua; #相对于nginx安装目录
}
}
在ngx根目录下的lua文件夹里创建“api”文件夹,并且在里面添加testController.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://xx/aaa"
local res, err = httpc:request_uri(url, {
method = "POST",
--args = str,
--body = "a=1&b=2",
body = "{'imei':'123456789'}",
headers = {
--自定义headers
["Content-Type"] = "application/json"
}
})
if not res then
ngx.log(ngx.WARN,"failed to request: ", err)
else
ngx.say(res.body);
end
需要下载依赖 http.lua http_headers.lua
下载地址 https://github.com/pintsized/lua-resty-http
存放至安装目录的 lualib\resty 即可
评论暂时关闭