nginx中content_by_lua和content_by_lua_file的学习

nginx中content_by_lua和content_by_lua_file的学习

原文参考:

http://wiki.nginx.org/HttpLuaModule#content_by_lua

content_by_lua

语法:content_by_lua <lua-script-str>

上下文:location或者location if

说明:作为一个”content handler”,为每一个请求去执行<lua-script-str>中的lua代码。这段lua代码可能执行其他API,并且在一个全新的独立全局环境中作为一个新的产生协同程序(spawned coroutine)被执行。
       注意:不要和其他内容处理命令在同一个location下使用,例如:content_by_lua不能和proxy_pass(反向代理)在同一个location下使用。

location /name/{
       content_by_lua <lua-script-str>;
       proxy_pass http://166.104.225.161;
}

这样是会产生错误的!!!!!!

content_by_lua_file

语法:content_by_lua_file <path-to-lua-script-file>

上下文:location或者location if

说明:和content_by_lua功能是一样的,不同的是,content_by_lua后面是具体的lua代码,而content_by_lua_file后面是lua文件。

      注意:这段话,我还不是很明白意思。

Nginx variables can be used in the <path-to-lua-script-file> string to provide flexibility. This however carries some risks and is not ordinarily recommended.

When a relative path like foo/bar.lua is given, they will be turned into the absolute path relative to the server prefix path determined by the -p PATH command-line option while starting the Nginx server.

When the Lua code cache is turned on (by default), the user code is loaded once at the first request and cached and the Nginx config must be reloaded each time the Lua source file is modified. The Lua code cache can be temporarily disabled during development by switching lua_code_cache off in nginx.conf to avoid reloading Nginx.

Nginx variables are supported in the file path for dynamic dispatch, for example:

 # WARNING: contents in nginx var must be carefully filtered,
    # otherwise there'll be great security risk!
    location ~ ^/app/([-_a-zA-Z0-9/]+) {
        set $path $1;
        content_by_lua_file /path/to/lua/app/root/$path.lua;
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注