这里主要使用日志的方式进行debug
环境依赖
项目对openresty包的依赖比较高,所以环境基础都在openresty下进行
openresty的使用
openresty下载地址
下载完成后解压,具体使用方式和nginx没有什么区别,主要依赖文件是一下几个
nginx.exe # 负责启动服务
conf/nginx.conf # nginx的配置文件 使用lua脚本主要也是在这里配置
logs # 日志查看 排错
lua链接mysql校验用户
这里拿lua链接mysql为例
-- utils_mysqllocal mysql = require "resty.mysql"
local cjson = require "cjson"
local _M = {}local function getUser(userNo, orgCode)db = _M:new()if not db thenreturn false, {}endlocal 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'")-- 目前用户不存在放行 只存入相关信息 设置匿名用户if not userRes thenngx.log(ngx.ERR, "用户--" .. userNo .. "-- 不存在" .. err)return false, {status = 401,message = "Bad token; " .. tostring(err)}endlocal 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 thenngx.log(ngx.ERR, "用户--" .. userNo .. "--和--" .. orgCode .. "--组织关系不存在"..err)return false, {status = 4060,message = "用户需要登录"}endngx.log(ngx.ERR, "orgUserRes Is" .. cjson.encode(orgUserRes))-- lua脚本似乎不遵循index=0的原则 [{"F_count":"1","F_status":"0"}]if orgUserRes[1]["F_status"] ~= '0' thenngx.log(ngx.ERR, "该组织下角色已被禁用,请联系管理员".. err)return false, {status = 7025,message = "该组织下角色已被禁用,请联系管理员"}endngx.log(ngx.WARN, "UserRes Is "..cjson.encode(userRes).. "orgUserRes Is" .. cjson.encode(orgUserRes))-- 校验成功的返回信息ngx.say('CheckUser Success')return true, {}
endfunction _M.new(self)local db, err = mysql:new()if not db thenngx.log(ngx.ERR, "failed to instantiate mysql: ", err)return nilend-- 1 secdb:set_timeout(1000)local ok, err, errcode, sqlstate = db:connect{host = "xxxx",port = "xxxx",database = "xxxx",user = "xxxx",password = "xxxx"}if not ok thenngx.log(ngx.ERR, "failed to connect: ", err, errcode, sqlstate)return nilendreturn db
endfunction _M.close(self)local sock = self.sockif not sock thenreturn nil, "not initialized"endif self.subscribed thenreturn nil, "subscribed state"end-- put it into the connection pool of size 100,-- with 10 seconds max idle timeoutlocal ok, err = self.sock:set_keepalive(10000, 100)if not ok thenngx.log(ngx.ERR, "failed to set keepalive:", err)returnend
endgetUser('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端口为8765listen 8765;server_name localhost;#charset koi8-r;# 添加的lua脚本校验路径location /testlua {default_type 'text/html'; # 默认文本方式返回charset utf-8;lua_code_cache off; # 不使用缓存content_by_lua_file luaScript/utils_mysql.lua; # 需要执行的lua脚本文件路径,这里的路径是相对于压缩包的路径,也可以使用绝对路径 注意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;# }#}
}
启动openresty
双击 nginx.exe即可,浏览器输入http://localhost:8765
进入启动页面
调试过程中,每次点击nginx.exe都会启动新的线程,导致有时候更新的项目但是打到的请求还是在老的配置服务上,为确保每次请求都是最新的,最好把线程都删掉
windows
# 展示线程
tasklist /fi "imagename eq nginx.exe"
# 杀掉线程
taskkill /fi "imagename eq nginx.exe" -f
Lua脚本校验
浏览器输入http://localhost:8765/testlua
进入插件校验页面
运行的日志和错误信息都可以在logs/access.log&error.log
中找到,由于这里已经是处理完成的脚本信息,如何解决问题这里不做展示。
需要注意的是,在脚本文件中ngx.log(ngx.ERR, "xxxx")
最好都设置成err级别,别的级别好像不会展示在日志中
校验
Error
Success
日志和错误信息都可以在logs/access.log&error.log
中找到,由于这里已经是处理完成的脚本信息,如何解决问题这里不做展示。
需要注意的是,在脚本文件中ngx.log(ngx.ERR, "xxxx")
最好都设置成err级别,别的级别好像不会展示在日志中
[外链图片转存中…(img-PpfLXDLJ-1689328661208)]
校验
Error
[外链图片转存中…(img-j6J54ZsA-1689328661209)]