這里主要使用日志的方式進(jìn)行debug
環(huán)境依賴
項(xiàng)目對(duì)openresty包的依賴比較高,所以環(huán)境基礎(chǔ)都在openresty下進(jìn)行
openresty的使用
openresty下載地址
下載完成后解壓,具體使用方式和nginx沒有什么區(qū)別,主要依賴文件是一下幾個(gè)
nginx.exe # 負(fù)責(zé)啟動(dòng)服務(wù)
conf/nginx.conf # nginx的配置文件 使用lua腳本主要也是在這里配置
logs # 日志查看 排錯(cuò)
lua鏈接mysql校驗(yàn)用戶
這里拿lua鏈接mysql為例
-- utils_mysql
local mysql = require "resty.mysql"
local cjson = require "cjson"
local _M = {}
local function getUser(userNo, orgCode)
db = _M:new()
if not db then
return false, {}
end
local userRes, err, errcode, sqlstate = db:query(
"SELECT COUNT(F_user_account) F_count FROM t_user WHERE F_user_account = " .. userNo .. " AND F_deleted='0'")
-- 目前用戶不存在放行 只存入相關(guān)信息 設(shè)置匿名用戶
if not userRes then
ngx.log(ngx.ERR, "用戶--" .. userNo .. "-- 不存在" .. err)
return false, {
status = 401,
message = "Bad token; " .. tostring(err)
}
end
local orgUserRes, err, errcode, sqlstate = db:query(
"SELECT COUNT(F_user_account) F_count, F_status FROM t_org_user WHERE F_user_account = " .. userNo .. " AND F_org_code = " ..
orgCode .. " AND F_deleted='0'")
if not orgUserRes then
ngx.log(ngx.ERR, "用戶--" .. userNo .. "--和--" .. orgCode .. "--組織關(guān)系不存在"..err)
return false, {
status = 4060,
message = "用戶需要登錄"
}
end
ngx.log(ngx.ERR, "orgUserRes Is" .. cjson.encode(orgUserRes))
-- lua腳本似乎不遵循index=0的原則 [{"F_count":"1","F_status":"0"}]
if orgUserRes[1]["F_status"] ~= '0' then
ngx.log(ngx.ERR, "該組織下角色已被禁用,請(qǐng)聯(lián)系管理員".. err)
return false, {
status = 7025,
message = "該組織下角色已被禁用,請(qǐng)聯(lián)系管理員"
}
end
ngx.log(ngx.WARN, "UserRes Is "..cjson.encode(userRes).. "orgUserRes Is" .. cjson.encode(orgUserRes))
-- 校驗(yàn)成功的返回信息
ngx.say('CheckUser Success')
return true, {}
end
function _M.new(self)
local db, err = mysql:new()
if not db then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
return nil
end
-- 1 sec
db:set_timeout(1000)
local ok, err, errcode, sqlstate = db:connect{
host = "xxxx",
port = "xxxx",
database = "xxxx",
user = "xxxx",
password = "xxxx"
}
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err, errcode, sqlstate)
return nil
end
return db
end
function _M.close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
if self.subscribed then
return nil, "subscribed state"
end
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = self.sock:set_keepalive(10000, 100)
if not ok then
ngx.log(ngx.ERR, "failed to set keepalive:", err)
return
end
end
getUser('username', 'org_code')
-- return _M
修改配置文件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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 修改原本的80端口為8765
listen 8765;
server_name localhost;
#charset koi8-r;
# 添加的lua腳本校驗(yàn)路徑
location /testlua {
default_type 'text/html'; # 默認(rèn)文本方式返回
charset utf-8;
lua_code_cache off; # 不使用緩存
content_by_lua_file luaScript/utils_mysql.lua; # 需要執(zhí)行的lua腳本文件路徑,這里的路徑是相對(duì)于壓縮包的路徑,也可以使用絕對(duì)路徑 注意windows下 \ 需要變成 /
}
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# }
#}
}
啟動(dòng)openresty
雙擊 nginx.exe即可,瀏覽器輸入http://localhost:8765
進(jìn)入啟動(dòng)頁面
調(diào)試過程中,每次點(diǎn)擊nginx.exe都會(huì)啟動(dòng)新的線程,導(dǎo)致有時(shí)候更新的項(xiàng)目但是打到的請(qǐng)求還是在老的配置服務(wù)上,為確保每次請(qǐng)求都是最新的,最好把線程都刪掉
windows
# 展示線程
tasklist /fi "imagename eq nginx.exe"
# 殺掉線程
taskkill /fi "imagename eq nginx.exe" -f
Lua腳本校驗(yàn)
瀏覽器輸入http://localhost:8765/testlua
進(jìn)入插件校驗(yàn)頁面
運(yùn)行的日志和錯(cuò)誤信息都可以在logs/access.log&error.log
中找到,由于這里已經(jīng)是處理完成的腳本信息,如何解決問題這里不做展示。
需要注意的是,在腳本文件中ngx.log(ngx.ERR, "xxxx")
最好都設(shè)置成err級(jí)別,別的級(jí)別好像不會(huì)展示在日志中
校驗(yàn)
Error
Success
日志和錯(cuò)誤信息都可以在logs/access.log&error.log
中找到,由于這里已經(jīng)是處理完成的腳本信息,如何解決問題這里不做展示。
需要注意的是,在腳本文件中ngx.log(ngx.ERR, "xxxx")
最好都設(shè)置成err級(jí)別,別的級(jí)別好像不會(huì)展示在日志中
[外鏈圖片轉(zhuǎn)存中…(img-PpfLXDLJ-1689328661208)]
校驗(yàn)
Error
[外鏈圖片轉(zhuǎn)存中…(img-j6J54ZsA-1689328661209)]文章來源:http://www.zghlxwxcb.cn/news/detail-596869.html
Success
文章來源地址http://www.zghlxwxcb.cn/news/detail-596869.html
到了這里,關(guān)于Lua腳本本地調(diào)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!