我想使用 Nginx 作为 HTTP 代理服务器。
在后端,我们有 3 个用 Java 编写的不同应用程序,每个应用程序都公开一个 RESTful API。每个应用程序在 API 上都有自己的前缀。
例如:
APP 1 - URI prefix: /api/admin/** APP 2 - URI prefix: /api/customer/** APP 3 - URI prefix: /api/support/**
在前端,我们有一个 SPA 页面向这些 URI 发出请求。
有没有办法告诉 Nginx 根据 URI 前缀路由 HTTP 请求?
提前致谢!
最佳答案
我很确定你可以使用 nginx 代理转发来根据你的几个 uri 前缀中的每一个重新路由。我在 nginx 中使用了代理转发。我从一个专门提供有关 uri 前缀的信息的页面改编了您的示例(我在此处保留了该页面的/foo 条目以供您比较):
https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite
另见(注意 proxy_pass 与 proxy_redirect 的区别), http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
使用您的代码,位置将类似于:
server {
listen 80;
server_name www.example.com;
location /api/admin {
proxy_pass http://localhost:3200/;
}
location /api/customer {
proxy_pass http://localhost:3200/;
}
location /api/support {
proxy_pass http://localhost:3200/;
}
location /foo {
proxy_pass http://localhost:3200/;
}
正如我提供的链接中提到的,请注意每个位置指令末尾的正斜杠,它在前缀后启用通配符。当然,每个路径重定向到的 url 不需要都相同 --localhost:3200--就像他们在这个例子中一样。
关于rest - Nginx 路由到多个 RESTful 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820013/