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

标签 api nginx port reverse-proxy

我有以下 API:

  1. 本地主机:300/api/customers/
  2. localhost:400/api/customers/:id/billing
  3. 本地主机: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;
    }
}

有什么问题需要修复吗?谢谢!

最佳答案

这三个服务由同一服务器代理(就 nginx 而言),因此必须在一个 server< 内将其构造为三个 location block block 。请参阅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语句相同,则可以在父 block 中指定一次。

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

除非您在此处终止 SSL,否则我不确定这是否有效。即配置reverse proxy as a secure server .

关于api - NGINX - 不同端口上的反向代理多个 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769963/

相关文章:

c - 端口更改后 UDP 客户端服务器无法交换消息

Javadoc生成错误

iphone - 在哪里可以找到 iPhone 应用程序的 JSON 天气 API?

android - 为什么我得到 Android : The target server failed to respond?

python - uWSGI和Nginx中MySQL连接出现502错误

php - aws + centos 7 + 内置 nginx ngx_pagespeed + php-fpm = 找不到文件

c - 如何找到任何PC的端口号?

javascript - 从 Node JS connect.js 获取服务器的端口

android - 用于 parking 位的 Google Place API

http-headers - 覆盖缓存控制 : Private in Nginx