NGINX Lua 请求体中的搜索/替换

因为一些遗留软件,我需要修改 SOAP 请求的请求体。它将 SOAP-ENV 用于命名空间,而实际上应该改为 'soap':

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body>

应该变成:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>

虽然这似乎没有关系,但对于遗留软件而言却是有影响的。为了至少进行快速修复,我考虑在请求体中进行搜索/替换。Nginx 在系统前充当 Web 服务器,所以我搜索了一下,发现只有 Lua 可以实现。我已经安装了 nginx-extras 并使用以下脚本:

        location ~ '\.php$' {
             location ~ ^/webservice/ {
               access_by_lua_block
               {
                   ngx.req.read_body()
                   local body = ngx.req.get_body_data()
                   if body
                      body = string.gsub(body, "SOAP-ENV", "soap")
                   end
                   ngx.req.set_body_data(body)
               }
             }
             fastcgi_split_path_info ^(.+?\.php)(|/.*)$;

             include fastcgi_params;
             fastcgi_param HTTP_PROXY "";
             fastcgi_param SCRIPT_FILENAME
             $document_root$fastcgi_script_name;
             fastcgi_param PATH_INFO $fastcgi_path_info;
             fastcgi_param QUERY_STRING $query_string;
             fastcgi_intercept_errors on;
             fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }

似乎请求体总是为空。我在使用 SOAP UI 发送 SOAP 请求。我做错了什么吗?

点赞
用户1985204
用户1985204
提醒自己:午餐时不要再快速阅读

if body 之后缺少 then,你想使用 ngx.re.gsub(我不太清楚为什么)。经过这两个更改,我在我的实验室中使它工作如下:

                   ngx.req.read_body()
                   local body = ngx.req.get_body_data()
                   if body then
                      body = ngx.re.gsub(body, "SOAP-ENV", "soap")
                   end
                   ngx.req.set_body_data(body)
2019-09-10 13:41:33
用户1619234
用户1619234

lua_need_request_body on; 使问题得以解决。请查看官方文档

2019-09-13 12:16:51