Nginx最酷的一点是,您可以通过在请求处理的各个阶段注入Lua脚本来控制它所做的事情。我已经成功地使用了rewrite_ by _lua/file指令来检查传入请求的主体,并为PHP下游处理注入额外的请求头。例如:
location /api{
rewrite_by_lua_file "/path/to/rewrite.lua";
lua_need_request_body "on";}
然后在rewrite.lua
local uri = ngx.var.request_uri;
//examine the URI and inject additional headers
ngx.req.set_header('headerName','headerValue');在这个阶段,我还可以做的是注入响应头。例如
local cookieData = "cookieName=value;path=/;";
ngx.header['Set-Cookie'] = cookieData;到目前为止还没有任何问题,但这并不是我想要做的。我想到的工作流程是这样的
通过我的PHP脚本注入额外的响应头完全不是问题。我想,为了检查那些额外的标题,我只需要设置
location /api {
header_filter_by_lua_file "path/to/header.lua";
}header.lua做了如下的事情
local cookieData = "cookieName=value;path=/;";
ngx.header['Set-Cookie'] = cookieData;这个原则听起来很完美。但是,我发现我的header_filter_by_lua_file指令被忽略了--在重新加载配置时,Nginx日志中没有报告错误。
我一定是在做错事,但我看不出可能是什么。我在Ubuntu14.10 (x64)上使用了x64。Nginx已经安装了apt-get install nginx-extras。
我将非常感谢任何能够解释如何使header_filter_by_*正常工作的人。
发布于 2015-12-02 05:01:31
这不是香草的特征。您需要安装openresty (而不是nginx)。
参见http://openresty.org/#Download,然后是http://openresty.org/#Installation
https://stackoverflow.com/questions/34025573
复制相似问题