OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。
今天用OpenResty + lua来遍历指定目录,返回json字符串
我们用Lua来遍历文件目录,并用nginx来访问lua文件,使其返回这个目录的json字符串。
Lua代码:
local lfs = require("lfs") function getType(path)
return lfs.attributes(path).mode
end function getSize(path)
return lfs.attributes(path).size
end function isDir(path)
return getType(path) == "directory"
end function findx(str,x)
for i = 1,#str do
if string.sub(str,-i,-i) == x then
return -i
end
end
end function getName(str)
return string.sub(str,findx(str,"/") + 1,-1)
end function getJson(path)
local table = "{"
for file in lfs.dir(path) do
p = path .. "/" .. file
if file ~= "." and file ~= '..' then
if isDir(p) then
s = "{'text':'".. file .. "','type':'" .. getType(p) .. "','path':'" .. p .. "','children':[]},"
else
s = "{'text':'".. file .. "','type':'" .. getType(p) .. "','path':'" .. p .. "','size':" .. getSize(p) .. ",'leaf':true},"
end
table = table .. s
end
end
table = table .. "}"
return table
end ngx.say(getJson('/var/www'))
这里遍历了"/var/www"目录下的所有文件及文件夹,并将其名字、类型、大小构成json字符串返回。ngx.say是Lua透露给模块的接口。
前面已经提过如何安装配置openresty,这里就不重复了。
接下来一步,我们在nginx下新建一个文件夹作为跟目录,比如在/usr/local/openresty/nginx/下建立一个work文件夹,work文件夹下建立logs文件夹用以存放日志文件,conf文件夹用以存放nginx配置文件。
然后在conf文件夹中新建一个nginx.conf配置文件。
worker_processes 2; error_log logs/error.log; events {
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost; location / {
root /usr/local/openresty/nginx/work;
index index.html index.htm;
}
}
}
其中:
worker_processes 数字代表nginx进程数,一般设置与自己电脑cpu内核数相同
error_log 指定了日志文件存放目录
worker_connections 单个进程最大连接数
在http中添加一个server,监听8080端口,其中添加了location,之后的"/"表示访问url的最后一部分,譬如"/" ==> localhost:8080/ "/a" ==> localhost:8080/a
location内的root指定了根目录,index指定了缺省访问顺序。
如果现在访问localhost:8080,将会访问/usr/local/openresty/nginx/work目录下的index.html文件。
我们可以用nginx -c 命令来启动指定的nginx配置文件。比如这里如果要启动nginx,命令为:
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/work/conf/nginx.conf
当然,这里必须保证8080端口没有被占用,否则就会发生:
可以用netstat -lnp | grep 8080命令来查看什么进程占用了8080端口,然后用kill命令给kill掉,最后再重启。
然后我们需要将Lua文件与nginx配合起来,在我们访问localhost:8080/dir时,将显示Lua生成的目录json字符串。
我们修改nginx.conf文件:
worker_processes 2; error_log logs/error.log; events {
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost; location / {
root /usr/local/openresty/nginx/work;
index index.html index.htm;
} location /dir {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/work/dir.lua;
}
}
}
当我们访问localhost:8080/dir时,将自动访问work文件夹下的dir.lua文件。
我们在/usr/local/openresty/nginx/work目录下新建dir.lua文件,内容为上面写过的内容。
最后重启nginx。
关于重启nginx,有两种方法,一种是用nginx -s reload,还有一种是kill掉nginx进程后,再nginx -c XXX/nginx.conf来启动指定nginx配置文件。
两者的区别在于,nginx -s 和nginx -t缺省判断nginx默认的配置文件,即:
/usr/local/openresty/nginx/sbin/nginx -t [-s reload]
命令判断与重载的配置文件为默认的/usr/local/openresty/nginx/conf/nginx.conf文件。
而第二种启动方法则可以启动指定目录下的配置文件,不过在启动前,需要保证没有nginx进程存在,所以一般需要先kill掉所有nginx进程,再用XXX/nginx -c XXX/nginx.conf来根据指定配置文件启动nginx。
这里我们用第二种方法启动nginx:
killall -9 nginx
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/work/conf/nginx.conf
启动成功后访问"localhost:8080/dir":