Nginx删除子字符串并添加为url参数

时间:2019-11-21 11:01:34

标签: nginx nginx-location

这与this question有关,但是答案对我不起作用。

我需要打开这个:/api/batch.json?param=1

进入/batch?param=1&format=json

Nginx位置:

location /api/batch {
   proxy_set_header   X-Real-IP        $remote_addr;
   proxy_set_header   Host             $http_host;
   proxy_pass         http://localhost:8000/batch;
}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

在使用rewrite...break将其传递到上游之前,使用location来更改proxy_pass中的URI。

例如:

location /api/batch {
    ...
    rewrite ^/api(/batch)\.(json)$ $1?format=$2 break;
    proxy_pass  ...;
}

除非替换字符串以rewrite结尾,否则?指令将自动附加原始参数(如果有)。有关详细信息,请参见this document