OpenResty安装与使用
nginx 配合lua实现服务器输入输出日志记录
- 安装环境: centos (具体版本不限)
- 安装前准备
必须将这些库 perl 5.6.1+, libpcre, libssl安装在您的电脑之中。 对于 Linux来说, 您需要确认使用 ldconfig 命令,让其在您的系统环境路径中能找到它们
yum install pcre-devel openssl-devel gcc curl
- 环境安装
你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum check-update 命令)。 运行下面的命令就可以添加我们的仓库(对于 CentOS 8 或以上版本,应将下面的 yum 都替换成 dnf):
# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
# update the yum index:
sudo yum check-update
然后就可以像下面这样安装软件包,比如 openresty:
sudo yum install -y openresty
如果你想安装命令行工具 resty,那么可以像下面这样安装 openresty-resty 包:
sudo yum install -y openresty-resty
命令行工具 opm 在 openresty-opm 包里,而 restydoc 工具在 openresty-doc 包里头。
列出所有 openresty 仓库里头的软件包:
sudo yum --disablerepo="*" --enablerepo="openresty" list available
- 测试demo
启动Nginx服务器;假设您已将OpenResty安装到/usr/local/openresty(这是默认设置),我们将OpenResty安装的nginx可执行文件在我们的环境中可用:PATH
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
为logs/为日志文件和conf/配置文件创建目录
mkdir /usr/local/openresty/nginx/conf/logs/ mkdir /usr/local/openresty/nginx/conf/conf/
然后分别创建日志文件和conf文件
vim /usr/local/openresty/nginx/conf/logs/error.log
#只需要创建一个就行,不用编辑内容
vim /usr/local/openresty/nginx/conf/conf/nginx.conf
#内容如下
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}
启动!!!
nginx -p `pwd`/ -c conf/nginx.conf
我们可以使用 curl 访问我们的新 Web 服务,上面写着 HelloWorld:
curl http://localhost:8080/
如果一切正常,我们应该得到输出
<p>hello, world</p>
- 记录接口输入输出
分别编写两个lua文件。关于ngx.var后常用属性,再最后配图
1.log.lua 用于记录输入参数,注意日志路径,自己修改
local request = "请求来了:====》".."["..ngx.var.time_local.."] ".."\""..ngx.var.request_method.." "..ngx.var.scheme.."://"..
ngx.var.host..ngx.var.request_uri.."\"".."请求参数".."{}".."请求时间:".."("..ngx.var.request_time..")".."客户端ip"..ngx.var.remote_addr
local f = assert(io.open("/usr/local/openresty/nginx/conf/lua/test.log", "a"))
f:write(request .. "\n")
--f:write(newcontent .. "\n")
f:close()
2.log1.lua用于记录输出日志,注意日志路径,自己修改
local newcontent = string.gsub(ngx.arg[1],"%c"," ")
local f = assert(io.open("/usr/local/openresty/nginx/conf/lua/test.log", "a"))
f:write(newcontent .. "\n")
f:close()
在nginx 中配置两个lua文件
#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 {
# init_by_lua_block {
# require "log1"
# }
lua_package_path "/usr/local/openresty/nginx/conf/lua/?.lua;;";
include mime.types;
default_type application/octet-stream;
client_body_buffer_size 128k;
client_max_body_size 128k;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
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;
}
}
server {
listen 8080;
set $log_val '';
location / {
default_type text/plain;
proxy_pass http://127.0.0.1:8001;
log_by_lua_file /usr/local/openresty/nginx/conf/lua/log1.lua;
body_filter_by_lua_file /usr/local/openresty/nginx/conf/lua/body_filter.lua;
}
}
}
- nginx的 ngx.var ngx.ctx ngx.req
ngx.var 是获取 Nginx 的变量,需要经历字符串 hash、hash 表查找等过程。
注意:在请求时候不要用原有接口请求了,需要用代理的端口
ngx.ctx 仅仅是一个 Lua table 而已,它的引用存放在 ngx_lua 的模块上下文(ctx_ref)。
使用 ngx.ctx 比 ngx.var 往往是更好的选择。
https://www.cnblogs.com/chenpython123/p/10832393.html