NGINX-在不同端口上反向代理多个API [英] NGINX - Reverse proxy multiple API on different ports
问题描述
我有以下API:
- localhost:300/api/customers/
- localhost:400/api/customers/:id/billing
- localhost:500/api/orders
我想使用NGINX使它们全部在以下位置运行:
I'd like to use NGINX to have them all run under the following location:
localhost: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
而言),因此必须在一个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屋!