Azure App Service on Linux (PHP) with Azure Front Door – Access Control Configuration

If you need to use Azure App Service on Linux with PHP code (like WordPress) behind Azure Front Door, which you should, then you need to secure communication between Azure Front Door and Azure App Service Web App. If you don’t want to or you cannot use custom Docker container, you are pinned to built-in PHP container in App Service on Linux.

In this configuration there is Apache Web Server (httpd) used to serve your app. If you scale out, you have as many Apache Web Servers as many instances in App Service Plan you had configured. At the front you have a load balancer. App Service Instances are NATed. App Services have inbound and outbound IP address pools and DNS name and domain. Azure Front Door is not integrating into App Service – it is totally separated from it.
But you are running Azure Front Door, because it is cool, and you are pointing to Azure App Service as a backend. And you want to service your website only through Azure Front Door, restricting the traffic.

It’s easy. You just need to restrict the traffic to the Web App only to traffic originating from Azure Front Door backend addresses. The list of all public IP classess used by Azure services is public and you can find it here. All you need to do is to find AzureFrontDoor.Backend value on the list of objects, create a JSON definition of ipSecurityRestrictions setting of your Web App. Don’t forget to add Azure’s basic infrastructure services (through virtualized host IP addresses: 168.63.129.16 and 169.254.169.254) and IPv6 address range, currently limited to 2a01:111:2050::/44.

{
  "ipSecurityRestrictions": [
      {
        "ipAddress": "13.73.248.16/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.37.64.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.37.156.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.37.195.0/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.37.224.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.38.84.72/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.38.136.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.39.11.8/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.41.4.88/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.41.64.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.41.192.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.42.4.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.42.129.152/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.42.224.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.43.41.136/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.43.65.128/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.43.130.80/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.45.112.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.45.192.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.150.160.96/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.189.106.112/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.192.161.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "20.192.225.48/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.67.48.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.74.30.72/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.80.56.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.80.168.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.80.184.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.82.248.248/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "40.89.16.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.12.41.8/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.12.193.8/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.104.25.128/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.105.80.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.105.88.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.107.48.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.107.144.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.120.40.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.120.224.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.137.160.112/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "51.143.192.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "52.136.48.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "52.140.104.104/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "52.150.136.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "52.228.80.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "102.133.56.88/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "102.133.216.88/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "147.243.0.0/16",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "191.233.9.120/29",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "2a01:111:2050::/44",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "168.63.129.16/32",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      },
      {
        "ipAddress": "169.254.169.254/32",
        "action": "Allow",
        "tag": "Default",
        "priority": 100
      }
    ]
}

But what if someone runs it’s own Azure Front Door and try to route to your Web App? It will work. You can configured it to work. What you want to do is to restrict the access only to YOUR Azure Front Door instance. It’s also easy and documented. All you need is to filter request headers for X-Azure-FDID header value and pass only those requests that match your Front Door ID. For PHP running on built-in App Service on Linux image, you can do it at the Apache Web Server layer. You need to create .htaccess file in your application’s repository (in the wwwroot directory) and add Apache2 Web Server directive to restrict access only to requests with X-Azure-FDID header pointing to your Azure Front Door instance:

<IfModule authz_core_module>
        <RequireAll>
                Require expr %{HTTP:X-Azure-FDID} == '{{FDID}}'
        </RequireAll>
</IfModule>

For other stacks you need to pick proper options – i.e. for Python image you can do it in your app’s code or try to do it at Gunicorn layer.

But how to determine Azure Front Door instance ID? It’s also easy. What you need to do is to send a GET request to Azure Resource Manager API (version 2020-01-01) for your Azure Front Door instance (by name). In the response you will find a “frontdoorId” property.

GET /subscriptions/{{subscription}}/resourceGroups/{{resource_group}}/providers/Microsoft.Network/frontdoors/{{front_door}}?api-version=2020-01-01 HTTP/1.1
Host: management.azure.com
Content-Type: application/json
Authorization: Bearer

About the Author:

Founder and animator of Polish Microsoft Azure User Group. Entrepreneur – expert and consultant in the field of adaptation and implementation of Microsoft Azure platform. Open Source Software and interoperability evangelist. Social activist and co-founder of the Aegis Foundation. Privately eternal student, enthusiast and a geek. Organiser and participant in conferences and events related to the integration of the IT community

Reference:

Smereczynski, M. (2020). Azure App Service on Linux (PHP) with Azure Front Door – Access Control Configuration. Available at: https://www.sharepointeurope.com/wp-admin/post-new.php [Accessed: 20th May 2020].

Check out more great Azure content here

Share this on...

Rate this Post:

Share:

Topics:

Azure