【问题标题】:Read form POST variables with nginx使用 nginx 读取表单 POST 变量
【发布时间】:2014-04-14 15:22:21
【问题描述】:

我有一个无法修改的客户端程序。它发出大型 POST (x-www-form-urlencoded) 请求,其中包含跨 WAN 链接的数百个变量,但我只需要其中的 5 个。我在本地客户端系统上插入 nginx 作为反向代理。最容易让 nginx 去除多余数据的方法是什么?

目前我看到的两种方式: 1.使用Lua(如果我做了,我是不是应该做content_by_lua,重写body,然后做一个子请求?还是有更简单的方法?) 2.使用form-input-nginx-module和proxy_set_body解析并抓取一些变量。

我已经在使用 OpenResty,所以 Lua 意味着没有额外的模块。但是,这可能意味着要编写更多的位置等等来执行子请求。

【问题讨论】:

    标签: nginx lua reverse-proxy openresty


    【解决方案1】:

    在我看来,最简单的方法是使用 lua。 content_by_lua、rewrite_by_lua、access_by_lua 或它们的任意组合之间的选择;将取决于您如何使用子请求的响应正文。该决定还将决定您是否需要其他地点。

    这里有几个例子:

    1. content_by_lua 以本地位置为目标。

    (这种方式需要定义子请求位置)

    location /original/url {
        lua_need_request_body on;
        content_by_lua '
            --Lots of params but I only need 5 for the subrequest
            local limited_post_args, err = ngx.req.get_post_args(5)
            if not limited_post_args then
                ngx.say("failed to get post args: ", err)
                return
            end            
            local subreq_uri = "/test/local"
            local subreq_response = ngx.location.capture(subreq_uri, {method=ngx.HTTP_POST, 
                                    body = ngx.encode_args(limited_post_args)})
            ngx.print(subreq_response.body)
        ';
    }    
    
    location ~/test/local {
        lua_need_request_body on;
        proxy_set_header Accept-Encoding "";
        proxy_pass http://echo.200please.com;
    }
    

    2。使用 rewrite_by_lua 到远程目标 (不需要额外的位置)

    location /original/url/to/remote {
        lua_need_request_body on;
        rewrite_by_lua '
            --Lost of params but I only need 5 for the subrequest
            local limited_post_args, err = ngx.req.get_post_args(5)
            if not limited_post_args then
                ngx.say("failed to get post args: ", err)
                return
            end                    
    
            --setting limited number of params
            ngx.req.set_body_data(ngx.encode_args(limited_post_args))
            --rewriting url 
            local subreq_path = "/test"
            ngx.req.set_uri(subreq_path)
        ';
        proxy_pass http://echo.200please.com;
    }   
    

    7 个参数限制为 5 个的示例发布请求:

    curl 'http://localhost/original/url/to/remote' --data 'param1=test&param2=2&param3=3&param4=4&param5=5&param6=6&param7=7' --compressed
    

    回复:

    POST /test HTTP/1.0
    Host: echo.200please.com
    Connection: close
    Content-Length: 47
    User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
    Accept: */*
    Accept-Encoding: deflate, gzip
    Content-Type: application/x-www-form-urlencoded
    
    param3=3&param4=4&param1=test&param2=2&param5=5
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2016-05-29
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 2017-03-28
      相关资源
      最近更新 更多