How can I manipulate the JSON body of a POST request using Nginx and Lua?

谁说胖子不能爱 提交于 2019-11-28 07:41:55

I will suggest you to use access_by_lua
in 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;
                -------
                ---------

in cache.lua file you can do something like :

if ngx.req.get_method() == "POST" then
    -- check if request method is POST 
    -- implement your logic 
    return
end
vdg

Adding to what Prashant already mentioned: when you download your Nginx configuration files from 3scale you will notice that there is a Lua file included. This file is already being called from access_by_lua_file.

That file would be in my opinion the best place to add your body manipulation code. It will be executed for every request before the proxy_pass to your API server is sent.

Also, this is a really good in-depth blog article about how to accomplish different transformation to your requests using Lua inside Nginx:

Recently I had to manipulate upstream based on JSON value in post request and I found this useful: NGINX LUA and JSON

It is basic configuration but gives an idea how to do it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!