NGINX - Reverse proxy multiple API on different ports

孤者浪人 提交于 2020-01-31 03:19:25

问题


I have the following API(s):

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

I'd like to use NGINX to have them all run under the following location:

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!


回答1:


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.

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;
    }
}

If the proxy_set_header statements are identical, they can be specified once in the parent block.

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.

I am not sure that this will work unless you terminate SSL here. That is configure the reverse proxy as a secure server.




回答2:


The accepted answer did not work for me; I had three servers running as Kubernetes service mapped to different Cluster IP's (accessible only from Kubernetes nodes). So I used the host machine IP - 10.ttt.ttt.104 and the following configuration so that I could access these services from my work network.

I am not an nginx expert by any length- but this worked -- so use it as base maybe

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/;
       }
     }
}


来源:https://stackoverflow.com/questions/39769963/nginx-reverse-proxy-multiple-api-on-different-ports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!