Play with Proxy in Azure Function App
Published Aug 24 2021 09:59 PM 18K Views
Microsoft

KevinLi_0-1627964796921.png

 

Proxy in Azure Function App is often used for specifying endpoints on your function app that are implemented by another resource. You can use these proxies to break a large API into multiple function apps (as in a microservice architecture), while still presenting a single API surface for clients. It acts like an API management in this way. It can also be used as a web proxy to redirect third-party website. Let's discover more interesting ways to play with it.

 

1. Mocking functions:

This is one of the most typical usages. There is no need to write the real code to get a customized response code, status and body. Within the function app, we can go ahead to Proxies and add a new proxy rule like as below easily:

KevinLi_5-1629296284982.png

 

Then we can test the result by sending GET/POST request to our defined endpoint using web browser or tools like Postman:

KevinLi_6-1629296284984.png

 

2. Refer endpoint in another resource:

We can specify an endpoint within current function app that are implemented by another resource e.g. function app by putting remote endpoint into the Backend URL, in below sample, when we hit https://yourfunctionapp.azurewebsites.net/api/HttpTrigger1, the proxy will hit https://anotherfunctionapp.azurewebsites.net/api/HttpTrigger1 in the backend. Also, if we put localhost in the backend URL, it is able to reach out to the API running in our local machine.

KevinLi_7-1629296284972.png

 

3. Transfer parameters:

Sometimes we would need to transfer some parameters like name or API key.

There are two ways:

  • Using route template, such as /api/HttpTrigger1/{name}, the parameter names are enclosed in braces {}.

KevinLi_8-1629296284973.png

  • Use request override to hardcode the parameter.

It is very useful when the remote endpoint requires an API key and we want it to be passed automatically and secretly without exposing it. In request override, we can add a query and put the key in plaintext there. But it is recommended to store these keys in application settings and referencing those in proxies for security reason. We can put the key in configuration and use symbol % to refer it, for example %apikey%.

KevinLi_9-1629296284975.png

 

4. Web Proxy:

  • Used as web proxy to show a single static page.

For example, we can set Route template to /bing and Backend URL to https://www.bing.com. Then we will get static homepage of remote website when access https://yourfunctionapp.azurewebsites.net/bing.

  • Proxy for all content from remote endpoint.

We can use wildcard in route template, such as /{*any} to represent the remaining path segments from the incoming request. The string is not fixed, you can also use {*dog}, {*cat} or anything you like.

KevinLi_0-1629358390440.png

  • Proxy for sub path

Sometimes you may notice the remote endpoint is using sub-path to get static files like pictures or JavaScript files. We can set proxy for them as well.

KevinLi_2-1629358655727.png

 

5. Advanced mode:

We can also manually edit proxies.json file and deploy it as part of our app when we use any of the deployment methods that Functions supports.

KevinLi_12-1629296284985.png

 

6. Disable local call

By default, Functions proxies use a shortcut to send API calls from proxies directly to functions in the same function app. This shortcut is used instead of creating a new HTTP request. We can disable that shortcut behavior by setting AZURE_FUNCTION_PROXY_DISABLE_LOCAL_CALL to true in application settings.

 

7. Decode slashes in backend URL

When AZURE_FUNCTION_PROXY_BACKEND_URL_DECODE_SLASHES is set to true, the URL example.com/api%2ftest resolves to example.com/api/test. By default, the URL remains unchanged as example.com/test%2fapi.

 

8. Introduction Video:

Below is our Azure Friday on Azure Function Proxies.

 

Thanks for reading this post. I hope you enjoyed it. Please feel free to write your comments and views about the same over here.

7 Comments
Co-Authors
Version history
Last update:
‎Aug 19 2021 12:39 AM
Updated by: