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

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

我该怎么做?

ppliang69 回答:Nginx删除子字符串并添加为url参数

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

例如:

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

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

本文链接:https://www.f2er.com/3058758.html

大家都在问