Openresty (also known as Ngx_openresty) is a full-featured WEB application server that packs standard Nginx cores, a lot of common third-party modules, and most of their dependencies.
Use Openresty + Lua today to iterate through the specified directory, returning a JSON string
We use LUA to traverse the file directory and use Nginx to access the LUA file, which returns the JSON string for that directory.
Lua Code:
1Local LFS = require ("LFS")2 3 functiongetType (path)4 returnlfs.attributes (path). Mode5 End6 7 functiongetsize (path)8 returnlfs.attributes (path). Size9 EndTen One functionisdir (path) A returnGetType (path) = = "Directory" - End - the functionfindx (str,x) - fori = 1, #str Do - ifString.sub (str,-i,-i) = =x Then - return-I + End - End + End A at functiongetName (str) - returnString.sub (Str,findx (str, "/") + 1,-1) - End - - functionGetjson (path) -Local table = "{" in forFileinchLfs.dir (PATH) Do -p = Path: "/".. file to ifFile ~= "." and file ~= '.. ' Then + ifIsdir (p) Then -s = "{' Text ': '". File: "', ' type ': '". GetType (P): "', ' path ': '". P.. "', ' children ': []}," the Else *s = "{' Text ': '". File: "', ' type ': '". GetType (P): "', ' path ': '". P.. "', ' Size ':". GetSize (P): ", ' leaf ': true}," $ EndPanax NotoginsengTable =table: S - End the End +Table = table. "}" A returnTable the End + -Ngx.say (Getjson ('/var/www '))
This iterates through all the files and folders in the "/var/www" directory and returns the name, type, and size of the JSON string. Ngx.say is the interface that LUA discloses to the module.
I've already mentioned how to install the configuration openresty, which is not repeated here.
In the next step, we create a new folder where we want it to be, for example, create a work folder under/root, create a logs folder under the Work folder to store the log files, and Conf folders to store Nginx configuration files.
Then create a new nginx.conf configuration file in the Conf folder.
1Worker_processes 2; 2 3Error_log Logs/error.log;4 5 Events {6Worker_connections 1024;7 }8 http {9 server {TenListen 8080; One server_name localhost; A -Location/ { -root/root/Work ; the index index.html index.htm; - } - } -}
which
Worker_processes number represents the number of nginx processes, the general setting is the same as the number of CPU cores
ERROR_LOG Specifies the log file storage directory
Worker_connections maximum number of connections for a single process
Add a server to HTTP, listen on port 8080, add location, and then "/" to access the last part of the URL, such as "/" ==> localhost:8080/"/a" ==> localhost:8080 /A
The root is specified in the location root, and index specifies the default access order.
If you access localhost:8080 now, you will be accessing the index.html file in the/root/work directory.
We can use the NGINX-C command to launch the specified nginx configuration file. For example, if you want to start Nginx, the command is:
Of course, it is important to ensure that port 8080 is not occupied, otherwise it will occur:
can be used NETSTAT-LNP | The grep 8080 command to see what process takes up 8080 ports, then kill with the KILL command, and then restart again.
Then we need to match the Lua file with Nginx, and when we access the Localhost:8080/dir, we will display the directory JSON string generated by LUA.
We modify the nginx.conf file:
1Worker_processes 2;2 3Error_log Logs/error.log;4 5 Events {6Worker_connections 1024;7 }8 http {9 server {TenListen 8080; One server_name localhost; A -Location/ { -root/root/Work ; the index index.html index.htm; - } - -Location/dir { +Default_type text/html; -Content_by_lua_file/root/work/dir.lua; + } A } at}
When we access Localhost:8080/dir, the Dir.lua file under the Work folder is automatically accessed.
We create a new Dir.lua file in the/root/work directory with content written above.
Finally restart Nginx
You can use the following command to see if nginx.conf has syntax errors:
/usr/local/openresty/nginx/sbin/nginx-t
Then restart:
/usr/local/openresty/nginx/sbin/nginx-s Reload
Then visit "Localhost:8080/dir":
"LUA" LUA + openresty traversal file directory