Nginx 捕获并重写所有以 /api 开头的请求到代理



我想将所有/api/(http://example.com/api/(的请求重定向到我的代理应用程序,并在此过程中删除/api。我试过这个,但我在卷曲时得到一个 404。它没有到达应用程序,我认为这与rewrite有关,但我不确定它是否正确。

location ~ ^/api {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://127.0.0.1:8070;
}

以下内容在硬编码时有效。它将我重定向到我的应用程序/error

location = /api/error {
proxy_pass http://127.0.0.1:8070/error;
}

例如,我希望http://example.com/api/login像这样http://example.com/login命中我的代理应用程序。

这对我有用。$1$is_args$args部分传递 URL 中/api之后的所有内容。现在,当命中我的域时,example.com/api/something/something传递给我的代理应用程序。

location ~/api(.*)$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8070$1$is_args$args;
}

相关内容