server { listen 8080; location /test { set $b "$a, world"; echo $b; } set $a hello; } 应该可以的, server-rewrite阶段 On 9月28日, 上午10时05分, junwei shi <alohad...@gmail.com> wrote: > RT > nginx是否支持在location外设置一个全局变量. > 代码里用content_by_lua_file 调用一些lua脚本。 > 但不想写相对路径,也不想多个地方写死同样的绝对路径。 > 我想是否可以支持配置一个base_path的全局变量 > 谢谢
Hello! 2012/9/27 junwei shi <aloh...@gmail.com>: > RT > nginx是否支持在location外设置一个全局变量. > 代码里用content_by_lua_file 调用一些lua脚本。 > 但不想写相对路径,也不想多个地方写死同样的绝对路径。 > 我想是否可以支持配置一个base_path的全局变量 content_by_lua_file 支持路径中包含 nginx 变量,所以可以这么干: set $mybase '/tmp'; location = /t { content_by_lua_file $mybase/a.lua; } 这样,执行的便是 /tmp/a.lua 了。 不过,一般推荐通过 nginx 启动命令行上的 -p 选项来指定 server prefix 路径,而 content_by_lua_file 处理相对路径是相对于 server prefix 的。例如,通过 nginx -p /tmp/ 命令启动 nginx 的话, content_by_lua a.lua; 也会使用 /tmp/a.lua,而不论你的 nginx 本身安装到了什么地方。 Best regards, -agentzh