NGINX - 在不同端口上反向代理多个 API [英] NGINX - Reverse proxy multiple API on different ports
问题描述
我有以下 API:
- 本地主机:300/api/customers/
- localhost:400/api/customers/:id/billing
- 本地主机:500/api/orders
我想使用 NGINX 让它们都在以下位置运行:
I'd like to use NGINX to have them all run under the following location:
本地主机:443/api/
localhost:443/api/
这似乎非常困难,因为客户跨越两台服务器.
This seems very difficult because of customers spanning two servers.
这是我从订单开始的失败尝试
Here's my failed attempt starting with orders
server {
listen 443;
server_name localhost;
location /api/orders {
proxy_pass https://localhost:500/api/orders;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 443;
server_name localhost;
location /api/customers/$id/billing {
proxy_pass https://localhost:400/api/customers/$id/billing;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 443;
server_name localhost;
location /api/customers {
proxy_pass https://localhost:300/api/customers;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
有什么可以解决的问题吗?谢谢!
Anything jump out as far as a fix? Thanks!
推荐答案
这三个服务是由同一个服务器代理的(就 nginx
而言)所以必须结构化为三个 location
块在一个 server
块内.有关详细信息,请参阅本文档.
The three services are being proxied by the same server (as far as nginx
is concerned) so must be structured as three location
blocks within one server
block. See this document for details.
如果您只是传递未修改的原始 URI,则无需在 proxy_pass
语句中指定 URI.
If you are just passing the original URI unmodified, you do not need to specify a URI on the proxy_pass
statement.
server {
{
listen 443;
server_name localhost;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location /api/orders {
proxy_pass https://localhost:500;
}
location /api/customers {
proxy_pass https://localhost:400;
}
location = /api/customers {
proxy_pass https://localhost:300;
}
}
如果 proxy_set_header
语句相同,它们可以在父块中指定一次.
If the proxy_set_header
statements are identical, they can be specified once in the parent block.
所需的 location
语句的类型取决于 localhost:300/api/customers/
服务处理的 URI 范围.如果它是一个 URI,则 =
语法将起作用.如果它是任何与 /api/customers/:id/billing
不匹配的 URI,那么您将需要使用正则表达式位置块.有关详细信息,请参阅本文档.
The type of location
statement required is dependent on the range of URIs processed by the localhost:300/api/customers/
service. If it is one URI, the =
syntax will work. If it is any URI that does not match /api/customers/:id/billing
, then you will need to use a regular expression location block. See this document for details.
除非您在此处终止 SSL,否则我不确定这是否有效.即配置反向代理作为安全服务器.
I am not sure that this will work unless you terminate SSL here. That is configure the reverse proxy as a secure server.
这篇关于NGINX - 在不同端口上反向代理多个 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!