是否可以使用Apache或Nginx重写SOAP POST请求正文? [英] Is it possible to rewrite SOAP POST request bodies with Apache or Nginx?

查看:104
本文介绍了是否可以使用Apache或Nginx重写SOAP POST请求正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to rewrite a SOAP POST request body and header, so that Apache or Nginx can proxy it to an application which resides on another server.

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

The request is composed like this, this one in particular refers to the "UploadDocument_v4" functionality:

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版本.因此,在 body AND header 中都应将_v4的所有引用都替换为_v2.

What I need to do, is to rewrite this request so that it invokes the same function, but with the _v2 version. So, all the references to _v4 should be replaced with _v2 both within body AND header.

这有可能吗?到目前为止,我一直使用Nginx和Apache,但如果可能的话,我还没有找到相关的模块.

Is this possible in any way? I used Nginx and Apache up until now but with the relative modules I haven't yet been able to figure out a way to do this, if possible.

感谢您可能提供的任何见解.

Thanks for any insights you might be able to provide.

更新: 正如Ivan在评论中所建议的那样,我尝试安装Openresty,并通过使用基本配置来确定它是否有效.但是,我似乎无法弄清楚如何替换正文和标题. nginx.conf文件如下:

UPDATE: As Ivan suggested in the comments, I tried to install Openresty, and by using a basic configuration, i'm able to ascertain that it is working. However, I can't seem to be able to figure out how to replace body and header. The nginx.conf file is below:

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中写点东西以来已经过去了两年,但我会尽力:)

Ok, it is about two years passed since I wrote something in Lua, but I'll try :)

    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指令,并且可能使用

I tested this config with curl and PHP script which print HTTP headers and POST request body, and it works. You can't use return 302 ... because your HTTP headers will be regenerated for a new request, you must use proxy_pass directive instead. Note that if you'll specify your proxy server by domain, not an IP address, you'll have to use proxy_set_header Host proxy-domain.com directive and possibly a resolver directive.

这篇关于是否可以使用Apache或Nginx重写SOAP POST请求正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆