Alex
Alex

Reputation: 519

Use Location to Redirect Nginx to Specific Docker Container

I have 3 .net core apis running in a docker containers orchestrated via docker compose behind a Nginx Reverse proxy.

I want to make it so that I can use 1 sub domain for all 3 apis but use different URI routes to redirect to each API respectively for example:

Docker Compose Service Names: API_A, API_B, API_C

URL in Browser -> Resource on Server

http://api.MyDomain.com/APIA/Swagger -> http://API_A:80/swagger

http://api.MyDomain.com/APIB/Swagger -> http://API_B:80/swagger

http://api.MyDomain.com/APIC/Swagger -> http://API_C:80/swagger

http://api.MyDomain.com/APIA/api/resource?query="a" -> http://API_A:80/resource?query="a"

http://api.MyDomain.com/APIB/api/resource?query="a" -> http://API_B:80/resource?query="a"

http://api.MyDomain.com/APIC/api/resource?query="a" -> http://API_C:80/resource?query="a"

Up until this point I have been using NGINX proxy manager UI to do this, but I think it will require some kind of nginx configuration that uses location.

Could somebody give me an example nginx config that accomplishes the above task?

I am not exactly sure what this operation is called. I would appreciate someone giving me the vocabulary that will allow me to better understand my needs and the nginx documentation.

Thanks for any advice.

Upvotes: 0

Views: 1727

Answers (1)

axel
axel

Reputation: 56

it seems what you want is request forwarding being done in reverse proxy nginx configuration.

You need to edit some lines in nginx.conf.

Here as per this config, your request sent at http://api.MyDomain.com/APIA/swagger will replace the location prefix matching the one present in config and forward it to http://API_A:80/swagger.

The configuration results in passing all requests processed in this location to the proxied server at the specified address.

  • Request to - http://{some ip/ domain}/APIA/xxxx
  • Forwards to - http://API_A:80/xxxx specified address written in proxy_pass

When the location prefix matching is done, the rest request is appended to proxy_pass address and rewritten in a form a new address.

server   { 
    listen 80;
    
    location  /APIA/api/ { 
      proxy_pass  http://API_A:80/;
    }
    location  /APIB/api/ { 
      proxy_pass  http://API_B:80/;
    }
    location  /APIC/api/ { 
      proxy_pass  http://API_C:80/;
    }
    location  /APIA/ { 
      proxy_pass  http://API_A:80/;
    }
    location  /APIB/ { 
      proxy_pass  http://API_B:80/;
    }
    location  /APIC/ { 
      proxy_pass  http://API_C:80/;
    }
    
  }

As per official doc - Nginx official doc

Note that in the first example above, the address of the proxied server is followed by a URI, /link/. If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. For example, here the request with the /some/path/page.html URI will be proxied to http://www.example.com/link/page.html. If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

Upvotes: 1

Related Questions