NGINX - 不同端口上的反向代理多个 API
NGINX - Reverse proxy multiple API on different ports
我有以下 API(s):
- localhost:300/api/customers/
- localhost:400/api/customers/:id/billing
- localhost:500/api/orders
我想使用 NGINX 将它们全部 运行 放在以下位置:
localhost:443/api/
这似乎很难,因为客户跨越两个服务器。
这是我从订单开始的失败尝试
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;
}
}
有什么可以解决的吗?谢谢!
这三个服务由同一台服务器代理(就 nginx
而言),因此必须在一个 server
块中构建三个 location
块。有关详细信息,请参阅 this document。
如果您只是传递未修改的原始 URI,则不需要在 proxy_pass
语句中指定 URI。
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
语句相同,则可以在父块中指定一次。
所需的 location
语句类型取决于 localhost:300/api/customers/
服务处理的 URI 范围。如果它是一个 URI,=
语法将起作用。如果它是任何不匹配 /api/customers/:id/billing
的 URI,那么您将需要使用正则表达式位置块。有关详细信息,请参阅 this document。
除非您在此处终止 SSL,否则我不确定这是否有效。即配置reverse proxy as a secure server.
已接受的答案对我不起作用;我有三台服务器 运行 作为映射到不同集群 IP 的 Kubernetes 服务(只能从 Kubernetes 节点访问)。所以我使用主机 IP - 10.ttt.ttt.104
和以下配置,以便我可以从我的工作网络访问这些服务。
我不是任何长度的 nginx 专家-但这有效-所以可以将它用作基础
events {
worker_connections 4096; ## Default: 1024
}
http{
server {
listen 80;
listen [::]:80;
server_name 10.ttt.ttt.104;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://10.103.152.188:80/;
}
}
server {
listen 9090;
listen [::]:9090;
server_name 10.ttt.ttt.104;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://10.107.115.44:9091/;
}
}
server {
listen 8080;
listen [::]:8080;
server_name 10.ttt.ttt.104;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://10.105.249.237:80/;
}
}
}
我有以下 API(s):
- localhost:300/api/customers/
- localhost:400/api/customers/:id/billing
- localhost:500/api/orders
我想使用 NGINX 将它们全部 运行 放在以下位置:
localhost:443/api/
这似乎很难,因为客户跨越两个服务器。
这是我从订单开始的失败尝试
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;
}
}
有什么可以解决的吗?谢谢!
这三个服务由同一台服务器代理(就 nginx
而言),因此必须在一个 server
块中构建三个 location
块。有关详细信息,请参阅 this document。
如果您只是传递未修改的原始 URI,则不需要在 proxy_pass
语句中指定 URI。
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
语句相同,则可以在父块中指定一次。
所需的 location
语句类型取决于 localhost:300/api/customers/
服务处理的 URI 范围。如果它是一个 URI,=
语法将起作用。如果它是任何不匹配 /api/customers/:id/billing
的 URI,那么您将需要使用正则表达式位置块。有关详细信息,请参阅 this document。
除非您在此处终止 SSL,否则我不确定这是否有效。即配置reverse proxy as a secure server.
已接受的答案对我不起作用;我有三台服务器 运行 作为映射到不同集群 IP 的 Kubernetes 服务(只能从 Kubernetes 节点访问)。所以我使用主机 IP - 10.ttt.ttt.104
和以下配置,以便我可以从我的工作网络访问这些服务。
我不是任何长度的 nginx 专家-但这有效-所以可以将它用作基础
events {
worker_connections 4096; ## Default: 1024
}
http{
server {
listen 80;
listen [::]:80;
server_name 10.ttt.ttt.104;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://10.103.152.188:80/;
}
}
server {
listen 9090;
listen [::]:9090;
server_name 10.ttt.ttt.104;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://10.107.115.44:9091/;
}
}
server {
listen 8080;
listen [::]:8080;
server_name 10.ttt.ttt.104;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://10.105.249.237:80/;
}
}
}