NGINX - 在不同端口上反向代理多个API

Pro*_*imo 12 api port reverse-proxy nginx

我有以下API:

  1. 本地主机:300/API /客户/
  2. 本地主机:400/API /客户/:ID /计费
  3. 本地主机:500/API /订单

我想使用NGINX将它们全部运行在以下位置:

本地主机: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;
    }
}
Run Code Online (Sandbox Code Playgroud)

什么东西跳出来修复?谢谢!

Ric*_*ith 17

这三个服务由同一服务器代理(就所nginx涉及的而言),因此必须location在一个块内构造为三个块server.请参阅此文档了解详细信息

如果您只是未经修改地传递原始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;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果proxy_set_header语句相同,则可以在父块中指定一次.

location所需的语句类型取决于localhost:300/api/customers/服务处理的URI范围.如果它是一个URI,=语法将起作用.如果它是任何不匹配的URI /api/customers/:id/billing,那么您将需要使用正则表达式位置块.请参阅此文档了解详细信息

除非你在这里终止SSL,否则我不确定这是否有效.这是将反向代理配置为安全服务器.


Ale*_*nen 7

接受的答案对我不起作用;我有三台服务器作为 Kubernetes 服务运行,映射到不同的集群 IP(只能从 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/;
       }
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 你所做的事情是不同的。我需要将不同端口上运行的服务合并到一个 SSL 端点中。 (2认同)