Hello!
I started a branch named "prefix-var" in ngx_lua's repository and
applied the patch from Matthieu Tourne to add support for the special
nginx variable $prefix in both the lua_package_path and
lua_package_cpath directives:
https://github.com/chaoslawful/lua-nginx-module/commits/prefix-var/
Note that the $prefix and ${prefix} syntax is only usable in this very
context and it is in fact just a syntax sugar. The value of the
$prefix variable is always evaluated to the "server prefix". The
"server prefix" is determined by the -p command-line option while
starting the nginx server. If the -p option is missing, it is just the
installation prefix while building nginx.
Web applications based on ngx_lua can benefit from this feature by
avoiding hard-coding absolute paths in nginx.conf. For example, the
web application's directory strucuture can look like this:
~/test/MyApp/
bin/
myapp
conf/
nginx.conf
logs/
lua/
api.lua
utils.lua
And then in conf/nginx.conf, we can just write:
lua_package_path "$prefix/lua/?.lua;;";
lua_package_cpath "$prefix/lua/?.so;;";
server {
...
location /api {
content_by_lua_file lua/api.lua;
}
location /api2 {
content_by_lua '
local utils = require "utils"
...
';
}
}
And in the nginx controlling script bin/myapp, we can start nginx like this:
/path/to/nginx -p $app_root/ -c conf/nginx.conf
where $app_root can be the root directory directory of the web
application (which can be just the current directory, $PWD). Here, the
nginx server prefix is set to the value "$app_root/", which can also
be used by the lua_package_path and lua_package_cpath directives.
Comments are very welcome on the changes in this "prefix-var" branch.
Regards,
-agentzh