Wednesday, July 1, 2015

How to configure nginx to handle same Rest URL for GET, POST, PUT in the same URI


Here the use case is there is a rest endpoint, and the incoming requests for the endpoint may have any type of HTTP methods.

Now the requirement is to configure the nginx to handle such scenarios.

Incoming url :- https://10.29.17.29:9443//api-gw/hello2/1.0/oooooogggggggggg


This url might be a GET or POST.

We can define the following nginx configuration for this purpose


upstream internallbgwhttps {
        server wso2.apimgw-cluster.com:8243;
}



server {

       listen 10.29.17.29:9443;
       server_name 10.29.17.29;

       ssl on;
       ssl_certificate     /usr/local/keys/self-ssl.crt;
       ssl_certificate_key /usr/local/keys/self-ssl.key;



 location ~^/api-gw/(.*)$ {

           access_log  /usr/local/whp/nginx/conf.d/logs/external-api-all-gw-access.log  main;

                   if ($request_method = GET){
                       rewrite  ^/api-gw/(.*)$  /gw-get/$1$is_args$args last;
           }

                   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_set_header Host $http_host;

           proxy_pass https://internallbgwhttps/$1;
           proxy_redirect  https://internallbgwhttps/(.*) https://10.29.17.29:9443/$1;
       }


           location ~^/gw-get/(.*)$ {

           access_log  /usr/local/whp/nginx/conf.d/logs/external-api-get-gw-access.log  main;


                   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_set_header Host $http_host;

                   proxy_pass https://internallbgwhttps/$1$is_args$args;
           proxy_redirect  https://internallbgwhttps/(.*) https://10.29.17.29:9443/$1;
       }


}

No comments:

Post a Comment