rest - Nginx 路由到多个 RESTful 端点

标签 rest http nginx routing http-proxy

我想使用 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/

相关文章:

node.js - REST API 权限/角色管理

java - 在 REST 调用中将 JSON 转换为资源

c# - 放置后如何从 philips hue 桥获取 http json 响应

http - DNS 请求(例如 txt 记录)是否可以用于提供 .css 和 .js 文件?

ssl - nginx ssl_certificate 指令在服务器 block 内不起作用,浏览器显示 ERR_CONNECTION_CLOSED 或 ERR_CONNECTION_RESET

nginx - 如何在将请求传递到上游服务器之前删除 Nginx 中的客户端 header ?

mysql - 无法获取 mysqli

c# - 使单个 WCF 服务同时支持 SOAP、REST 和 WSDL

ruby-on-rails - "Create"路由路径助手

c# - 在 .NET 中从 URL 读取字符串的最简单方法