在这篇文章之后,我使用Lua来增加nginx的灵活性,并减lessweb栈的负载。我很想知道人们如何使用Lua来增强nginx的function。
有没有什么显着的黑客,优化和观察使用Lua ? 人们曾经用Nginx来发现function的文章,否则在Web服务器或反向代理中会变得复杂/不可能?
编辑:
链接:
http://thechangelog.com/post/3249294699/super-nginx-killer-build-of-nginx-build-for-luajit-plus
http://skillsmatter.com/podcast/home/scripting-nginx-with-lua/te-4729
http://devblog.mixlr.com/2012/06/26/how-we-use-nginx-lua-and-redis-to-beta-ify-mixlr/
之前我们已经编写了一个解决scheme,根据指定的域或域别名dynamic获取文档根目录。 它使用一个mysql数据库来存储域名到别名映射。 链接到它是http://www.logicwreck.com/index.php/2012/09/11/dynamic-hosts-for-nginx-with-database-storage-of-domain-and-alias-info/
一个非常有趣的黑客,因为它可以防止你有多个configuration的虚拟主机,而只有一个。
我的文档根解决scheme也使用Lua,但使用bash脚本而不是数据库。 简而言之,它颠倒了域的部分,并将它们用作目录结构。 在此结构中,以_开头的目录可以用来区分文档根目录和子目录。
例如domain.com – > com / domain / _public sub.domain.com – > com / domain / sub / _public
server_name _ ~^(?<www>www\.)?(?<domain>[a-zA-Z0-9-\.]+)$; set_by_lua $docRoot " local f = assert(io.popen('/path/to/conversion/script.sh '..ngx.var.domain, 'r')) local s = assert(f:read('*a')) f:close() return s "; root /var/www/$docRoot/_public;
我的转换脚本是:
echo $1 | tr "." "\n" | tac | tr "\n" "/" | rev | cut -b 2- | rev | tr -d "\n"
剪切删除额外的斜杠,而最后的tr是需要的,因为在nginx根中有一个换行符会截断/ _public
我运行了几十个这样的域,没有任何问题。 它可能比每个域的基于模板的configuration生成器慢,但我更喜欢有一个文件来排除故障。