openresty-安装

OpenResty = Nginx + Lua,是一个增强的Nginx,可以编写lua脚本实现非常灵活的逻辑了

1.安装开发库依赖

yum install -y pcre-devel openssl-devel gcc curl

2.配置yum的依赖源

yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

3.安装OpenResty

yum install openresty

4.openresty的默认安装目录

/usr/local/openresty

5.启动openresty(Nginx)

/usr/local/openresty/nginx/sbin/nginx
或
systemctl restart openresty

6.通过浏览器查看nginx的页面

Welcome to OpenResty!

If you see this page, the OpenResty web platform is successfully installed and working. Further configuration is required.

For online documentation and support please refer to our openresty.org site
Commercial support is available at openresty.com.

We have articles on troubleshooting issues like high CPU usage and large memory usage on our official blog site.

Thank you for flying OpenResty.

扫描二维码关注公众号,回复: 14747170 查看本文章

nginx结合lua的(openresty)

修改openresty下nginx的配置文件

vi /usr/local/openresty/nginx/conf/nginx.conf

使用lua的log_by_lua_file将参数写入到指定的日志文件中

location /log.gif {
	#伪装成gif文件
	default_type image/gif;    
	#本身关闭access_log
	access_log off;    
	lua_code_cache off;
  #使用lua将nginx的接收的参数写入到日志文件中
  log_by_lua_file 'conf/log.lua';
  
	#返回空图片
	empty_gif;
}

在nginx的conf目录下创建一个log.lua文件

vi /usr/local/openresty/nginx/conf/log.lua

编写lua脚本

-- 获取请求参数列表
local request_args_tab = ngx.req.get_uri_args()
-- 定义一个json对象
local log_json = {}
local length = 0
-- 将参数的K和V迭代出来,添加到json对象中
for k, v in pairs(request_args_tab) do
    log_json[k] = v
    length = length + 1
end
if (length > 0)
then
  -- 引入lua用来解析json的库
  local cjson = require "cjson" 
  -- lua_code_cache off;
  local time = os.date("%Y%m%d%H",unixtime)
  -- 使用lua的io打开一个文件,如果文件不存在,就创建,a为append模式
  local path = "/logs/access-" .. time .. ".json"
  local file = io.open(path, "a")
  -- 将json写入到指定的log文件,末尾追加换行
  file:write(cjson.encode(log_json), "\n")
  -- 将数据写入
  file:flush()
end

创建存放日志的目录并设置写入权限,然后在浏览器访问 ,查看logs下日志文件内容。

http://localhost/log.gif?id=123

猜你喜欢

转载自blog.csdn.net/robinhunan/article/details/129776079