NGINX Lua在请求正文中进行搜索/替换

时间:2019-09-10 07:52:25

标签: 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请求。我在这里做什么错了?

2 个答案:

答案 0 :(得分:2)

[自我说明:午餐时不再需要快速阅读]

then之后缺少if body,而您想使用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)

答案 1 :(得分:0)

lua_need_request_body on;达到了目的。参见官方documentation