【问题标题】:How to change request parameters before passing request to nginx reverse proxy server如何在将请求传递给 nginx 反向代理服务器之前更改请求参数
【发布时间】:2016-03-19 04:09:58
【问题描述】:

我正在使用 Nginx 将请求代理到某个后端服务器。 为此,我使用 proxy_pass 模块进行重定向。 配置文件: 位置 = /你好 { proxy_pass: abc.com }

我想执行以下工作流程。 向 nginx 服务器请求 --> 更改请求参数 --> 将请求传递给 abc.com --> 更改响应参数 --> 将响应发送回客户端。

这可以用 nginx 吗?任何有关此问题的帮助/指针将不胜感激。

【问题讨论】:

    标签: nginx reverse-proxy nginx-location


    【解决方案1】:

    您应该能够使用此更改/设置新参数

    location /hello {
      proxy_pass: abc.com 
      if ($args ~* paramToChange=(.+)) {
        set $args newParamName=$1;
      }
      set $args otherParam=value;
    }
    

    更新:

    开箱即用的 nginx 没有办法发出请求以动态获取参数,然后在发送客户端响应之前将它们应用于另一个请求。

    您可以通过向 nginx 添加一个名为 lua 的模块来做到这一点。

    这个模块可以通过下载并在安装过程中添加到.configure选项中重新编译成nginx。我也喜欢它附带的 openresty 捆绑包和其他已经可用的有用模块,例如 echo。

    一旦你有了 lua 模块,这个服务器代码就可以工作了:

    server {
        listen 8083;
    
        location /proxy/one {
            proxy_set_header testheader test1;
            proxy_pass http://localhost:8081;
        }
    
        location /proxy/two {
           proxy_pass http://localhost:8082;
        }
    
        location / {
            default_type text/html; 
    
            content_by_lua '
                local first = ngx.location.capture("/proxy/one",
                   { args = { test = "test" } }
                )
    
                local testArg = first.body
    
                local second = ngx.location.capture("/proxy/two",
                   { args = { test = testArg } }
                )
    
                ngx.print(second.body)
            ';
    
        }
    }
    

    我用几个这样的节点 js 服务器测试了这个配置:

    var koa = require('koa');
    var http = require('http');
    
    startServerOne();
    startServerTwo();
    
    function startServerOne() {
      var app = koa();
    
    
      app.use(function *(next){
        console.log('\n------ Server One ------');
        console.log('this.request.headers.testheader: ' + JSON.stringify(this.request.headers.testheader));
        console.log('this.request.query: ' + JSON.stringify(this.request.query));
    
        if (this.request.query.test == 'test') {
          this.body = 'First query worked!';
        }else{
          this.body = 'this.request.query: ' + JSON.stringify(this.request.query);
        }  
      });
    
      http.createServer(app.callback()).listen(8081);
    
      console.log('Server 1 - 8081');
    }
    
    function startServerTwo(){
      var app = koa();
      app.use(function *(next){
    
        console.log('\n------ Server Two ------');
        console.log('this.request.query: ' + JSON.stringify(this.request.query));
    
        if (this.request.query.test == 'First query worked!') {
          this.body = 'It Worked!';
        }else{
          this.body = 'this.request.query: ' + JSON.stringify(this.request.query);
        }
      });
    
      http.createServer(app.callback()).listen(8082);
    
      console.log('Server 2 - 8082');
    }
    

    这是节点控制台日志的输出:

    Server 1 - 8081
    Server 2 - 8082
    
    ------ Server One ------
    this.request.headers.testheader: "test1"
    this.request.query: {"test":"test"}
    
    ------ Server Two ------
    this.request.query: {"test":"First query worked!"}
    

    会发生什么:

    1. Nginx 向服务器一发送带有测试参数集的请求查询。

    2. 节点服务器 1 看到测试参数并响应“第一个查询成功!”。

    3. Nginx 使用服务器响应的正文更新查询参数。 Nginx 向服务器 2 发送带有新查询参数的请求。

    4. 节点服务器 2 发现“测试”查询参数等于“第一个查询成功!”并使用响应正文“It Worked!”响应请求。

    curl 响应或在浏览器中访问 localhost:8083 显示“成功”:

    curl -i 'http://localhost:8083'
    HTTP/1.1 200 OK
    Server: openresty/1.9.3.2
    Date: Thu, 17 Dec 2015 16:57:45 GMT
    Content-Type: text/html
    Transfer-Encoding: chunked
    Connection: keep-alive
    
    It Worked!
    

    【讨论】:

    • 新参数本质上是动态的。我需要调用某种网络调用或服务调用来填充该参数。另外我想在调用代理传递之前设置一些参数。
    • 我在上面添加了更新。第一个服务器调用可以替换为您需要的网络或服务调用。来自第一台服务器的响应用于在调用第二台服务器之前设置参数。
    猜你喜欢
    • 2020-06-14
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多