OpenResty安装与使用nginx配合lua实现服务器输入输出日志记录

时间:2025-03-02 07:33:19

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 /package/centos/
sudo mv  /etc//

# 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/
#只需要创建一个就行,不用编辑内容
vim /usr/local/openresty/nginx/conf/conf/

#内容如下

worker_processes  1;
error_log logs/;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ("<p>hello, world</p>")
            }
        }
    }
}

启动!!!

nginx -p `pwd`/ -c conf/

我们可以使用 curl 访问我们的新 Web 服务,上面写着 HelloWorld:

curl http://localhost:8080/

如果一切正常,我们应该得到输出

<p>hello, world</p>
  • 记录接口输入输出

分别编写两个lua文件。关于后常用属性,再最后配图

用于记录输入参数,注意日志路径,自己修改


  
local request = "请求来了:====》".."["...time_local.."] ".."\""...request_method.." "...."://"..
                     ...request_uri.."\"".."请求参数".."{}".."请求时间:".."("...request_time..")".."客户端ip"...remote_addr
local f = assert(("/usr/local/openresty/nginx/conf/lua/", "a"))
f:write(request .. "\n")
--f:write(newcontent .. "\n")
f:close()

2.用于记录输出日志,注意日志路径,自己修改

local newcontent = ([1],"%c"," ")
local f = assert(("/usr/local/openresty/nginx/conf/lua/", "a"))
f:write(newcontent .. "\n")
 
f:close()

在nginx 中配置两个lua文件


#user  nobody;
worker_processes  1;

#error_log  logs/;
#error_log  logs/  notice;
#error_log  logs/  info;

#pid        logs/;

events {
    worker_connections  1024;
}


http {
#	init_by_lua_block {
#		require "log1"
#	}
	 lua_package_path "/usr/local/openresty/nginx/conf/lua/?.lua;;";
 	 include       ;
    	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   ;
        }

        #error_page  404              /;

        # redirect server error pages to the static page /
        #
        error_page   500 502 503 504  /;
        location = / {
            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/;
		body_filter_by_lua_file /usr/local/openresty/nginx/conf/lua/body_filter.lua;
	   	
		}
	}
	
   

}
  • nginx的
    是获取 Nginx 的变量,需要经历字符串 hash、hash 表查找等过程。
    注意:在请求时候不要用原有接口请求了,需要用代理的端口

仅仅是一个 Lua table 而已,它的引用存放在 ngx_lua 的模块上下文(ctx_ref)。

使用 比 往往是更好的选择。
/chenpython123/p/