您应该能够使用此更改/设置新参数
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!"}
会发生什么:
Nginx 向服务器一发送带有测试参数集的请求查询。
节点服务器 1 看到测试参数并响应“第一个查询成功!”。
Nginx 使用服务器响应的正文更新查询参数。
Nginx 向服务器 2 发送带有新查询参数的请求。
节点服务器 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!