Thursday, May 25, 2017

How to configure reverse proxy?

I'm going to discuss two different reverse proxy configurations

Use case 01:

If someone wants to configure a reverse proxy service to hide URI of the actual backend API and allow client to invoke the particular resource URI using a different resource URI the configuration would be as follow. 

API URL that the user invokes
http://host:port/test/customer/v1/xyz
API URL exposed by the API Manager

http://host:port/customer/v1/xyz
Sample Nginx configuration

upstream apimworker{
      server 10.100.11.22:8280;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server{
      listen 8280;
      server_name gw.wrk.wso2.com;
      underscores_in_headers on;

       #chunked_transfer_encoding off;

location /test/customer {
       proxy_set_header X-Forwarded-Host $host;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_http_version 1.1;
       proxy_pass http://apimworker/customer/;
       proxy_redirect http://apimworker/customer/ http://gw.wrk.wso2.com:8280/customer/;

        }
}

In the sample Nginx configuration I have also configured load balancing configurations together with proxy redirect. See How Nginx process requests for information on request processing. 


Use case 02:

In this scenario when ever the client sends a URI that matches the URI pattern defined in the first location,  the particular URL is being rewritten (reconstructed) to match the actual backend URI. In the second location we have defined how actual URI begins. As the reconstructed URI matches the URI defined in the second location the request gets passes to the backend.

API URL that the user invokes

http://host:port/test/{path_parameter}/customer/search
API URL exposed by the API Manager

http://host:port/customer/search/v1
Sample Nginx configuration
server {
   listen       81;
   server_name  gateway.apim.com;

     location ~ /test/.*/customer/search.* {
     rewrite ^.*/(customer/search/)(.*)$ /$1v1/$2 last;
   }

     location  /customer/search {
     proxy_pass http://localhost:8000;
   }

Below I have listed a sample URL to identify how above configurations works.

API URL that the user invoke: 
http://gateway.apim.com:81/test/1/customer/search/xyz

API URL send to the backend by the reverse proxy:
http://localhost:8000/customer/search/xyz




No comments:

Post a Comment