【问题标题】:NGINX - Reverse proxy multiple API on different portsNGINX - 在不同端口上反向代理多个 API
【发布时间】:2017-02-07 18:13:39
【问题描述】:

我有以下 API:

  1. localhost:300/api/customers/
  2. localhost:400/api/customers/:id/billing
  3. localhost:500/api/orders

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

有什么可以解决的吗?谢谢!

【问题讨论】:

    标签: api nginx port reverse-proxy


    【解决方案1】:

    这三个服务由同一台服务器代理(就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

    【讨论】:

      【解决方案2】:

      接受的答案对我不起作用;我有三台服务器作为 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/;
             }
           }
      }
      

      【讨论】:

      • 你所做的是不同的。我需要将在不同端口上运行的服务合并到一个 SSL 端点。
      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2020-08-12
      • 2012-11-04
      • 2017-05-14
      • 2020-04-16
      • 2019-11-04
      相关资源
      最近更新 更多