VMware Tanzu Archive

 View Only

 Proxying calls from a Single page app to a REST backend

Vinay Bhat's profile image
Vinay Bhat posted Mar 06, 2019 06:58 AM

I have an Angular app thats deployed with Staticfile buildpack. I need to add some proxy paths so it can hit a REST backend thats deployed separately. I tried to reference a file that has these additional location paths in the Staticfile like this: location_include: locs.conf and added the locations to the locs.conf file . 

   But, when i push the app, it isnt able to create a droplet and fails. Can someone please let me know what the best practise is for a detached front end application to talk to a REST backend and how to accomplish this? 

Stephen Carter's profile image
Stephen Carter

Hi Vinay,

 

I was able to proxy calls by adding a file "nginx/conf/includes/proxy.conf" with required proxy config.

Then make nginx read the file by updating "Staticfile" with an includes directive.

 

Example:

List of files: ./index.html ./Staticfile ./nginx/conf/includes/proxy.conf     $ cat Staticfile location_include: includes/*.conf     $ cat ./nginx/conf/includes/proxy.conf location /google { proxy_pass https://google.ie/; }

After doing a push and sending a request to "MYAPP/google" I got the Google homepage returned.

 

 

Kind regards,

Stephen

Vinay Bhat's profile image
Vinay Bhat

Thanks Stephen. I was able to get it working. The next challenge I am facing is since the code will be deployed to multiple environments, the proxy_pass will be different per environment. My thought was to look at the referer header within the location directive and proxy to the right backend server. But, NGINX strongly prohibits using "if" conditions within the location directive. How would I accomplish this with the Staticfile buildpack? From what I see, Staticfile doesnt let you define new server virtual hosts. Maybe thats a limitation of the staticfile and i need to look at another buildpack ?

Any thoughts would be highly appreciated!

Thanks

Vinay Bhat's profile image
Vinay Bhat

I was able to get this working with NGINX buildpack. Essentially i defined 2 server roots (virtual hosts) and in each, the server_name is the environment. I then put the location rules within each server root and set the proxy_pass to the right environment.