我正在做一个概念证明来演示我们如何在堆栈中实现3scale.在一个示例中,我想做一些POST 请求主体操作来创建API外观,该外观将可能是旧API格式的内容映射到新的内部API格式.例如.改变像
{ "foo" : "bar" , "deprecated" : true }
Run Code Online (Sandbox Code Playgroud)
成
{ "FOO" : "bar" }
Run Code Online (Sandbox Code Playgroud)
该Lua的模块文档的content_by_lua,这似乎是适当的方法说
不要在同一位置使用此指令和其他内容处理程序指令.例如,不应在同一位置使用此指令和proxy_pass指令.
我的理解是content_by_lua是一个像proxy_pass这样的内容处理程序,每个位置只能使用其中一个.
我不认为有任何方法可以删除proxy_pass,因为这是代理工作方式的基础,因此可以在单独的位置捕获请求,使用content_by_lua,然后传递到实现proxy_pass的位置,或者是否有不同的方法,如rewrite_by_lua哪个更合适?
如果它对其他人有帮助,我添加了以下函数(我的第一个Lua),它删除了user_key3scale需要授权的参数,但如果转发到我们的API则无效:
function remove_user_key()
ngx.req.read_body()
-- log the original body so we can compare to the new one later
local oldbody = ngx.req.get_body_data()
log(oldbody)
-- grab the POST parameters as a table
local params = ngx.req.get_post_args()
-- build up the new JSON string
local newbody = "{"
for k,v in pairs(params) do
-- add all the params we want to keep
if k ~= "user_key" then
log("adding"..k.." as "..v)
newbody = newbody..'"'..k..'":"'..v..'",'
else
log("not adding user_key")
end
end
--remove the last trailing comma before closing this off
newbody = string.sub(newbody, 0, #newbody-1)
newbody = newbody.."}"
ngx.req.set_body_data(newbody)
log(newbody)
end
if ngx.req.get_method() == "POST" then
remove_user_key()
end
Run Code Online (Sandbox Code Playgroud)
我建议你access_by_lua
在nginx.conf中 使用
location / {
#host and port to fastcgi server
default_type text/html;
set $URL "http://$http_host$request_uri";
access_by_lua_file /home/lua/cache.lua;
proxy_pass http://$target;
-------
---------
在cache.lua文件中,您可以执行以下操作:
if ngx.req.get_method() == "POST" then
-- check if request method is POST
-- implement your logic
return
end
| 归档时间: |
|
| 查看次数: |
19121 次 |
| 最近记录: |