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

问题描述 投票:2回答:1

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;
}

我该怎么做?

nginx nginx-location
1个回答
0
投票

在使用rewrite...break将其传递到上游之前,先使用locationproxy_pass中更改URI。

例如:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.