Nginx的autoindex 命令可以自动列出目录下的文件,一些网站用这个功能做文件下载,但是Nginx又没有提供这个页面的
自定义
的功能,后来看到别人提及 ngx_openresty,才想到body_filter
阶段可以修改 response 的请求内容,只要进行一些hacking就能完成这个需求啦。
Use openresty
to modify the page generated by the nginx autoindex
command
code
nginx 配置片段
location /autoindex {
alias conf/html/ ;
autoindex on;
body_filter_by_lua_file conf/lua/autoindex.lua;
}
autoindex.lua
-- 改变下auto index的输出内容
local html = ngx.arg[1]
-- add js, 也可以使用 echo_after_body 指令
-- html = html.."<script src='/bootstrap/js/sqlite.js'></script>";
-- add css
--ngx.arg[1] = ngx.re.gsub(html, "(<head>)", "$0 <link type=\"text/css\" rel='stylesheet' href='/sqlite/css.css'/>")
-- 修改title
ngx.arg[1] = ngx.re.gsub(html, "<h1>(.+)</h1>", "<h1>EryaOS Download</h1>")
感谢春哥