浏览器给openresty连接发送参数请求,查询数据库,并返回json数据
nginx.conf配置文件
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
log_format log_resp_body '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $bytes_sent $request_length "$request_body" "$resp_body"';
lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c模块
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server_tokens off;
#gzip on;
# lua
#lua_shared_dict limit 10m;
#lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
#init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
#access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
server {
listen 80;
server_name 192.168.0.174;
#charset koi8-r;
access_log logs/host.access.log log_resp_body;
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
location / {
root html;
index index.html index.htm;
}
location =/hello {
default_type 'text/plain';
content_by_lua 'ngx.say("欢迎使用Lua")';
}
location /lua {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/test.lua;
}
location /lua_var {
default_type 'text/plain';
content_by_lua_block {
ngx.say(ngx.var.arg_b)
}
}
location /lua_request {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_request.lua;
}
location /lua_response {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_response.lua;
}
location = /lua_log{
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_log.lua;
}
location /lua_sum {
internal;
content_by_lua_block {
local args = ngx.req.get_uri_args()
ngx.say(tonumber(args.a) + tonumber(args.b))
}
}
location = /lua_sum_test {
content_by_lua_block {
local res = ngx.location.capture("/lua_sum", {args={a=3, b=8}})
ngx.say("status:", res.status, " response:", res.body)
}
}
location /lua_redirect {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/lua_redirect.lua;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location ~ /lua_cjson {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/test_cjson.lua;
}
location /lua_mysql {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/nginx/conf/lua_conf/test_mysql.lua;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
test_mysql.lua文件
--[[
说明:
1.传递单个参数进行查询
2.实际使用需要修改数据库连接信息,查询表的信息
3.操作流程大致是获取传递过来的参数,连接数据库,构造查询sql,然后以json格式返回查询的结果
--]]
-- 引用外部lua文件模块
local mysql = require("resty.mysql")
local cjson = require("cjson")
local db, err = mysql:new()
if not db then
ngx.say("nfailed to instantiate mysql: ", err)
return
end
--设置数据库连接超时时间,1 sec
db:set_timeout(1000)
local props = {
host = "192.168.0.254",
port = 3306,
database = "hkd",
user = "hkd",
password = "Hkd123456;",
charset = "utf8",
max_packet_size = 1024 * 1024,
}
local ok, err, errcode, sqlstate = db:connect(props)
if not ok then
ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
-- return close_db(db)
end
-- ngx.say("connected to mysql.")
-- 获取get参数
-- 1.链接样式:http://192.168.0.175/lua_mysql?name=111
-- 2.curl样式:curl http://192.168.0.175/lua_mysql?name=111
-- 3.postman软件:http://192.168.0.175/lua_mysql?name=111 (在Params中添加参数)
local arg = ngx.req.get_uri_args()
for k,v in pairs(arg) do
local select_sql = string.format("select * from jeecg_demo where %s=%s" ,k,v) -- 字符串格式化
res, err, errcode, sqlstate = db:query(select_sql)
if not res then
ngx.say("select error : ", err, " , errcode : ", errcode, " , sqlstate : ", sqlstate)
return close_db(db)
end
end
--获取post参数
-- 1.curl样式:curl http://192.168.0.175/lua_mysql -X POST -d "name=111"
-- 2.postman软件:http://192.168.0.175/lua_mysql (在x-www-form-urlencoded中添加参数)
ngx.req.read_body()
local arg = ngx.req.get_post_args()
for k,v in pairs(arg) do
-- ngx.say("[POST] key:", k, " v:", v)
local select_sql = string.format("select * from jeecg_demo where %s=%s" ,k,v) -- 字符串格式化
res, err, errcode, sqlstate = db:query(select_sql, 10)
if not res then
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
end
-- 把查询结果包装成json返回
ngx.say("result: ", cjson.encode(res))
local ok, err = db:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
-- 关闭数据库连接
local function close_db(db)
if not db then
return
end
db:close()
-- ngx.say("success to close db: ", err)
end
close_db(db)
openresty下lualib目录结构
[root@bogon openresty]# tree lualib/
lualib/
├── cjson.so
├── librestysignal.so
├── ngx
│ ├── balancer.lua
│ ├── base64.lua
│ ├── errlog.lua
│ ├── ocsp.lua
│ ├── pipe.lua
│ ├── process.lua
│ ├── re.lua
│ ├── resp.lua
│ ├── semaphore.lua
│ ├── ssl
│ │ └── session.lua
│ └── ssl.lua
├── redis
│ └── parser.so
├── resty
│ ├── aes.lua
│ ├── core
│ │ ├── base64.lua
│ │ ├── base.lua
│ │ ├── ctx.lua
│ │ ├── exit.lua
│ │ ├── hash.lua
│ │ ├── misc.lua
│ │ ├── ndk.lua
│ │ ├── phase.lua
│ │ ├── regex.lua
│ │ ├── request.lua
│ │ ├── response.lua
│ │ ├── shdict.lua
│ │ ├── time.lua
│ │ ├── uri.lua
│ │ ├── utils.lua
│ │ ├── var.lua
│ │ └── worker.lua
│ ├── core.lua
│ ├── dns
│ │ └── resolver.lua
│ ├── limit
│ │ ├── conn.lua
│ │ ├── count.lua
│ │ ├── req.lua
│ │ └── traffic.lua
│ ├── lock.lua
│ ├── lrucache
│ │ └── pureffi.lua
│ ├── lrucache.lua
│ ├── md5.lua
│ ├── memcached.lua
│ ├── mysql.lua
│ ├── random.lua
│ ├── redis.lua
│ ├── sha1.lua
│ ├── sha224.lua
│ ├── sha256.lua
│ ├── sha384.lua
│ ├── sha512.lua
│ ├── sha.lua
│ ├── shell.lua
│ ├── signal.lua
│ ├── string.lua
│ ├── upload.lua
│ ├── upstream
│ │ └── healthcheck.lua
│ └── websocket
│ ├── client.lua
│ ├── protocol.lua
│ └── server.lua
└── tablepool.lua
浏览器给openresty连接发送参数请求,查询数据库,并返回json数据的更多相关文章
- 请求*.html后缀无法返回json数据的问题
在springmvc中请求*.html不可以返回json数据. 修改web.xml,添加url拦截格式.
- laravel 查询数据库first()返回的数据转数组
使用 get_object_vars()可以将他抓转为数组get_object_vars — 返回由对象属性组成的关联数组: 在laravel中其实还可以用 toArray(); json_decod ...
- javaweb Servlet接收Android请求,并返回json数据
1.实现功能 (1)接收http请求 (2)获取Android客户端发送的参数对应的内容 (3)hibernate查询数据库 (4)返回json数据 2.java代码 import EntityCla ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- jQuery通过Ajax向PHP服务端发送请求并返回JSON数据
SON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.JSON在前后台交互的过程中发挥着相当出色的作用.请接着往下看教 ...
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
[正文] 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层 ...
- 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)
在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...
随机推荐
- Ubuntu18.04安装rabbitvcs svn图形化客户端和简单实用
1.1 自带source源里面查找rabbitvcs信息 sudo apt search rabbitvcs 1.2 安装rabbitvcs sudo apt install rabbitvcs- ...
- Linux如何永久打开端口
由于防火墙导致同局域网无法通过IP访问,Linux有多种防火墙,需要查看当前使用的防火墙(开机自启),再进行配置 以下是 iptables 和 firewall 防火墙的相关配置,切忌将自己配置的防 ...
- Jmeter -- 添加断言,及断言结果
步骤: 1. 添加响应断言(添加-断言-响应断言) Add --> Assertions --> Response Assertion 2. 配置断言 判断响应内容中,是否包含关键字“禅 ...
- echarts之bootstrap选项卡不能显示其他标签echarts图表
在echarts跟bootstrap选项卡整合的时候,默认第一个选中选项卡可以正常加载echarts图表,但是切换其他选项的时候不能渲染出其他选项卡echarts图表. 解决方法: 在js中添加代码: ...
- 第三天·HTML常用标签
一·<h1>-<h6> 单词缩写:headHTML的<h1>-<h6>代表了六个等级的标题,其中<h1>标签比较重要,因此要尽量少用.一般& ...
- Python 入门知识
一python 语言介绍 特点 简单清晰明确 目前是全球最火的高级编程语言 应用领域很广 NASA FACEBOOK 谷歌 春雨医生 金山 腾讯,Python是一门解释性弱类型编程语言.编译性得 ...
- js对象深拷贝、浅拷贝
浅拷贝1 //浅拷贝1 let obj01 = { name: 'Lily', age: '20', time: ['13', '15'], person: { name: 'Henry', age: ...
- LeetCode19----删除链表的指定元素
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- EBS 页面影藏“关于此页”
EBS环境: R12.1.3 问题:要影藏EBS登录页面左下角的“关于此页” 方法: 修改的配置文件参数:FND:诊断 , 由 是 改为 否 个性化自助定义 ,由 是 改为 否参数说明:‘FND:诊断 ...
- bloom filter小结
Bloom Filter是由 Howard Bloom在 1970 年提出的一种多哈希函数映射的快速查找算法,它是一种空间效率很高的随机数据结构,利用位数组很简洁地表示一个集合,并能判断一个元素是否属 ...