是否可以使用Apache或Nginx重写SOAP POST请求体



我正在尝试重写SOAP POST请求bodyheader,以便Apache或Nginx可以将其代理到另一台服务器上的应用程序。

请求是这样组成的,这个特别指的是"UploadDocument_v4"功能:

POST / HTTP/1.1
Accept-Encoding: gzip, deflate
Content-Type: application/soap+xml;charset=UTF-8;action="http://server.workstepController.Process/UploadDocument_v4"
Host: 192.168.1.2
Content-Length: 245508
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Connection: close
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:com="http://server.workstepController.Process/">
<soap:Header/>
<soap:Body>
<com:UploadDocument_v4>
<!--Optional:-->
<com:workstepId></com:workstepId>
<!--Optional:-->
<com:customizationId></com:customizationId>
<!--Optional:-->
<com:document>OMISSIS</com:document>
<!--Optional:-->
<com:fileName>contract.pdf</com:fileName>
<com:timeToLive>100</com:timeToLive>
<!--Optional:-->
<com:transactionInformation><![CDATA[
<!--Further information for this transaction.-->
<TransactionInformation>
<!--The client id.-->
<ClientId>myClientId</ClientId>
<!--The transaction id.-->
<TransactionId>1234567890</TransactionId>
<!--Specifies if the currentWorkstepInformation should be returned by the method.-->
<ReturnWorkstepInfo>1</ReturnWorkstepInfo>
<!--The product name.-->
<ProductName />
<!--The product version.-->
<ProductVersion />
<!--The product release date in format YYYY-MM-DD.-->
<ProductReleaseDate />
<!--The email of the user.-->
<UserId />
<!--The ip address of the user.-->
<IpAddress />
</TransactionInformation>
]]></com:transactionInformation>
</com:UploadDocument_v4>
</soap:Body>
</soap:Envelope>

我需要做的是重写这个请求,以便它调用相同的函数,但使用_v2版本。因此,对_v4的所有引用都应替换为_v2,这两个引用都位于正文和标头中。

这有可能吗?到目前为止,我使用了Nginx和Apache,但对于相关模块,如果可能的话,我还没能找到实现这一点的方法。

感谢您提供的任何见解。

更新:正如Ivan在评论中建议的那样,我尝试安装Openresty,通过使用基本配置,我能够确定它是否正常工作。然而,我似乎不知道如何更换身体和头部。nginx.conf文件如下:

worker_processes 1;
error_log logs/error.log
events{
worker_connections 1024;
}
http{
server{
listen 8080;
location / {
--default_type text/html;
rewrite_by_lua_block{
--This is not being substituted
ngx.req.set_header("Content-Type", "[...]/UploadDocument_v2")
};
a = ngx.req.read_body();
a = string.gsub(a,"_v4","_v2");
return 302 http://192.168.1.3:1234;
}
}
}

好吧,我用Lua写东西已经两年了,但我会试试:(

location / {
access_by_lua '
ngx.req.read_body()
local body = ngx.req.get_body_data()
body = string.gsub(body, "_v4", "_v2")
ngx.req.set_body_data(body)
local header = ngx.req.get_headers()["Content-Type"]
header = string.gsub(header, "_v4", "_v2")
ngx.req.set_header("Content-Type", header)
';
proxy_pass http://192.168.1.3:1234;
}

我用curl和PHP脚本测试了这个配置,它们打印HTTP头和POST请求体,并且它可以工作。不能使用return 302 ...,因为将为新请求重新生成HTTP标头,必须使用proxy_pass指令。请注意,如果要按域而不是IP地址指定代理服务器,则必须使用proxy_set_header Host proxy-domain.com指令,也可能使用resolver指令。

最新更新