Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx routing to several RESTful endpoints

I would like to use Nginx as the HTTP proxy server.

On the backend, we have 3 different applications written in Java exposing a RESTful API each. Every application has their own prefix on the API.

For instance:

APP 1 - URI prefix: /api/admin/**
APP 2 - URI prefix: /api/customer/**
APP 3 - URI prefix: /api/support/**

On the frontend, we have a SPA page making requests to those URI's.

Is there a way to tell Nginx to route the HTTP request depending on the URI prefix?

Thanks in advance!

like image 283
dfanaro Avatar asked Jun 14 '16 18:06

dfanaro


People also ask

Can NGINX act as API gateway?

One advantage of using NGINX as an API gateway is that it can perform that role while simultaneously acting as a reverse proxy, load balancer, and web server for existing HTTP traffic. If NGINX is already part of your application delivery stack then it is generally unnecessary to deploy a separate API gateway.

Can NGINX be used as forward proxy?

Nginx is often used as a load balancer, a reverse proxy, and an HTTP Cache, among other uses. In this tutorial, we are focusing on learning how to use it as a forward proxy for any requested location.

What is proxy_pass NGINX?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

Does NGINX support gRPC?

The ability to route gRPC requests based on package, service, or RPC method means that existing NGINX functionality can be applied to gRPC traffic in exactly the same way as for HTTP/REST APIs, or indeed as for regular web traffic.


1 Answers

I am pretty sure you can use nginx proxy forwarding to re-route according to each of your several uri prefixes. I have used proxy forwarding with nginx. Your example I have adapted from a page that gives information specifically on uri prefixes (I have preserved the /foo entry from that page here for your comparison):

https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite

See also (noting difference of proxy_pass from proxy_redirect), http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

With your code, the locations would be something like:

server {
listen          80;
server_name     www.example.com;

location /api/admin {
  proxy_pass http://localhost:3200/;
}

location /api/customer {
  proxy_pass http://localhost:3200/;
}

location /api/support {
  proxy_pass http://localhost:3200/;
}

location /foo {
  proxy_pass http://localhost:3200/;
}

}

As the link I have provided mentions, note the forward slash at the end of each location directive that enables the wild carding after the prefix. And of course the urls to which each of the paths are redirected need not all be the same--localhost:3200--as they are in this example.

like image 123
Ben Weaver Avatar answered Sep 28 '22 07:09

Ben Weaver