Application Gateway では、App Service アプリまたはその他のマルチテナント サービスをバックエンド プール メンバーとして使用できます。 この記事では、Application Gateway を使用して App Service アプリを構成する方法について説明します。 Application Gateway の構成は、App Service にアクセスする方法によって異なります。
- 1 つ目のオプションでは、Application Gateway とバックエンドの App Service の両方でカスタム ドメインを使います。
- 2 つ目のオプションでは、Application Gateway は、既定のドメインに ".azurewebsites.net" というサフィックスを付けたものを使って App Service にアクセスします。
この構成は、運用レベルのシナリオに推奨され、要求フローでホスト名を変更しないというプラクティスを満たします。 既定の ".azurewebsites" ドメインに依存しなくて済むよう、カスタム ドメイン (および関連付けられた証明書) を使用できる必要があります。
バックエンド プールの Application Gateway と App Service の両方に同じドメイン名を関連付けることにより、要求フローでホスト名をオーバーライドする必要がありません。 バックエンド Web アプリケーションは、クライアントによって使用された元のホストが認識されます。
この構成は最も簡単であり、カスタム ドメインを必要としません。 そのため、短時間で簡単にセットアップできます。
App Service にカスタム ドメインが関連付けられていない場合、Web アプリケーションに着信する要求のホスト ヘッダーには、".azurewebsites.net" というサフィックスの付いた既定のドメインが設定されている必要があります。そうなっていない場合、プラットフォームは要求を適切にルーティングできません。
Application Gateway によって受信された元の要求のホスト ヘッダーは、バックエンドの App Service のホスト名とは異なります。
この記事では、以下を行う方法について説明します。
- DNS を構成する
- App Service をバックエンド プールとして Application Gateway に追加する
- App Service への接続用に HTTP の設定を構成する
- HTTP リスナーを構成する
- 要求のルーティング規則を構成する
前提条件
DNS の構成
このシナリオのコンテキストでは、DNS は 2 つの場所で関連があります。
- ユーザーまたはクライアントが Application Gateway に対して使用しており、ブラウザーに表示される DNS 名
- Application Gateway がバックエンドの App Service にアクセスするために内部的に使用している DNS 名
カスタム ドメインを使って、ユーザーまたはクライアントを Application Gateway にルーティングします。 Application Gateway 用の DNS を指す CNAME の別名を使って DNS を設定します。 Application Gateway の DNS アドレスは、関連付けられたパブリック IP アドレスの概要ページに表示されます。 または、IP アドレスを直接指す A レコードを作成します。 (Application Gateway V1 の場合、サービスを停止して開始すると VIP が変わる可能性があるため、このオプションは望ましくありません)。
着信ホストとしてカスタム ドメイン名を使って、Application Gateway からのトラフィックを受け入れるように、App Service を構成する必要があります。 カスタム ドメインを App Service にマップする方法について詳しくは、「チュートリアル: 既存のカスタム DNS 名を Azure App Service にマップする」をご覧ください。ドメインを検証するには、App Service で TXT レコードを追加することだけが必要です。 CNAME または A レコードを変更する必要はありません。 カスタム ドメインの DNS 構成は、Application Gateway を指し示した状態のままになります。
App Service への HTTPS 経由の接続を受け入れるには、その TLS バインドを構成します。 詳しくは、「Azure App Service で TLS/SSL バインドを使用してカスタム DNS 名をセキュリティで保護する」をご覧ください。カスタム ドメインの証明書を Azure Key Vault からプルするように App Service を構成します。
カスタム ドメインを利用できない場合、ユーザーまたはクライアントは、ゲートウェイの IP アドレスまたはその DNS アドレスを使って、Application Gateway にアクセスできます。 Application Gateway の DNS アドレスは、関連付けられたパブリック IP アドレスの概要ページで確認できます。 カスタム ドメインを利用できないということは、Application Gateway の TLS に対して使用できるパブリックに署名された証明書がないことを意味します。 クライアントは、HTTP または HTTPS と自己署名証明書を使うしかありませんが、どちらも望ましくありません。
App Service に接続するため、Application Gateway は App Service によって提供された既定のドメイン ("azurewebsites.net" というサフィックスが付いたもの) を使います。
バックエンド プールとして App Service を追加する
Azure portal で、お使いのアプリケーション ゲートウェイを選びます。
[バックエンド プール] で、バックエンド プールを選択します。
[ターゲットの種類] で、[App Services] を選択します。
[ターゲット] で、目的の App Service を選択します。
Note
このドロップダウンに表示されるのは、お使いの Application Gateway と同じサブスクリプションにある App Service だけです。 Application Gateway とは異なるサブスクリプションにあるアプリ サービスを使う場合は、[ターゲット] ドロップダウンで [App Services] を選ぶ代わりに、[IP アドレスまたはホスト名] オプションを選び、アプリ サービスのホスト名 (example.azurewebsites.net) を入力します。
[保存] を選択します。
# Fully qualified default ___domain name of the web app:
$webAppFQDN = "<nameofwebapp>.azurewebsite.net"
# For Application Gateway: both name, resource group and name for the backend pool to create:
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
$appGwBackendPoolNameForAppSvc = "<name for backend pool to be added>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Add a new Backend Pool with App Service in there:
Add-AzApplicationGatewayBackendAddressPool -Name $appGwBackendPoolNameForAppSvc -ApplicationGateway $gw -BackendFqdns $webAppFQDN
# Update Application Gateway with the new added Backend Pool:
Set-AzApplicationGateway -ApplicationGateway $gw
App Service の HTTP 設定を編集する
カスタム ドメイン名を使って App Service バックエンドにアクセスするよう Application Gateway に指示する HTTP の設定が必要です。 HTTP 設定では、既定では既定の正常性プローブを使用します。 既定の正常性プローブでは、トラフィックが受信されるホスト名を使用して要求を転送しますが、ホスト名が明示的に定義されていないため、正常性プローブではバックエンド プールへのホスト名として 127.0.0.1 を利用します。 このため、そのホスト名として正しいカスタム ドメイン名を使用して構成されているカスタムの正常性プローブを作成する必要があります。
ここでは、HTTPS を使ってバックエンドに接続します。
- [HTTP 設定] で、HTTP の既存の設定を選ぶか、新しく追加します。
- HTTP の設定を新しく作成するときは、名前を指定します
- 使用するバックエンド プロトコルとして、ポート 443 を使う HTTPS を選びます
- 証明書が既知の機関によって署名されている場合は、[既知の CA 証明書を使用する] で [はい] を選びます。 または、バックエンド サーバーの認証証明書または信頼されたルート証明書を追加します
- [新しいホスト名でオーバーライドする] は、必ず [いいえ] に設定します
- [カスタム プローブ] のドロップダウンで、カスタム HTTPS 正常性プローブを選びます。
既定の ("azurewebsites.net") ドメイン名を使って App Service バックエンドにアクセスするよう Application Gateway に指示する HTTP の設定が必要です。 これを行うため、HTTP の設定でホスト名を明示的にオーバーライドします。
- [HTTP 設定] で、HTTP の既存の設定を選ぶか、新しく追加します。
- HTTP の設定を新しく作成するときは、名前を指定します
- 使用するバックエンド プロトコルとして、ポート 443 を使う HTTPS を選びます
- 証明書が既知の機関によって署名されている場合は、[既知の CA 証明書を使用する] で [はい] を選びます。 または、バックエンド サーバーの認証証明書または信頼されたルート証明書を追加します
- [新しいホスト名でオーバーライドする] を、必ず [はい] に設定します
- [ホスト名をオーバーライドする] で、[バックエンド ターゲットからホスト名を選択する] をオンにします。 この設定により、App Service への要求で、バックエンド プールで構成されているとおりの "azurewebsites.net" ホスト名が使われるようになります。
# Configure Application Gateway to connect to App Service using the incoming hostname
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
$customProbeName = "<name for custom health probe>"
$customDomainName = "<FQDN for custom ___domain associated with App Service>"
$httpSettingsName = "<name for http settings to be created>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Add custom health probe using custom ___domain name:
Add-AzApplicationGatewayProbeConfig -Name $customProbeName -ApplicationGateway $gw -Protocol Https -HostName $customDomainName -Path "/" -Interval 30 -Timeout 120 -UnhealthyThreshold 3
$probe = Get-AzApplicationGatewayProbeConfig -Name $customProbeName -ApplicationGateway $gw
# Add HTTP Settings to use towards App Service:
Add-AzApplicationGatewayBackendHttpSettings -Name $httpSettingsName -ApplicationGateway $gw -Protocol Https -Port 443 -Probe $probe -CookieBasedAffinity Disabled -RequestTimeout 30
# Update Application Gateway with the new added HTTP settings and probe:
Set-AzApplicationGateway -ApplicationGateway $gw
# Configure Application Gateway to connect to backend using default App Service hostname
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
$httpSettingsName = "<name for http settings to be created>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Add HTTP Settings to use towards App Service:
Add-AzApplicationGatewayBackendHttpSettings -Name $httpSettingsName -ApplicationGateway $gw -Protocol Https -Port 443 -PickHostNameFromBackendAddress -CookieBasedAffinity Disabled -RequestTimeout 30
# Update Application Gateway with the new added HTTP settings and probe:
Set-AzApplicationGateway -ApplicationGateway $gw
トラフィックを受け入れるには、リスナーを構成する必要があります。 これについて詳しくは、「Application Gateway リスナーの構成」をご覧ください。
- [リスナー] セクションを開き、[リスナーの追加] を選ぶか、既存のものをクリックして編集します
- 新しいリスナーの場合: 名前を指定します
- [フロントエンド IP] で、リッスンする IP アドレスを選びます
- [ポート] で [443] を選びます
- [プロトコル] で [HTTPS] を選びます
- [証明書の選択] で、[キー コンテナーから証明書を選択する] を選びます。 詳しくは、Key Vault の使用に関する記事をご覧ください。マネージド ID を割り当てて、Key Vault への権限を提供する方法について、詳しく説明されています。
- 証明書の名前を指定します
- マネージド ID を選びます
- 証明書を取得するキー コンテナーを選びます
- 証明書を選択する
- [リスナーの種類] で [基本] を選びます
- [追加] をクリックしてリスナーを追加します
使用可能なカスタム ドメインがない場合、または証明書が関連付けられていない場合を想定し、ポート 80 で HTTP トラフィックをリッスンするよう Application Gateway を構成します。 または、自己署名証明書を作成する方法についての説明をご覧ください
- [リスナー] セクションを開き、[リスナーの追加] を選ぶか、既存のものをクリックして編集します
- 新しいリスナーの場合: 名前を指定します
- [フロントエンド IP] で、リッスンする IP アドレスを選びます
- [ポート] で [80] を選びます
- [プロトコル] で [HTTP] を選びます
# This script assumes that:
# - a certificate was imported in Azure Key Vault already
# - a managed identity was assigned to Application Gateway with access to the certificate
# - there is no HTTP listener defined yet for HTTPS on port 443
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
$appGwSSLCertificateName = "<name for ssl cert to be created within Application Gateway"
$appGwSSLCertificateKeyVaultSecretId = "<key vault secret id for the SSL certificate to use>"
$httpListenerName = "<name for the listener to add>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Create SSL certificate object for Application Gateway:
Add-AzApplicationGatewaySslCertificate -Name $appGwSSLCertificateName -ApplicationGateway $gw -KeyVaultSecretId $appGwSSLCertificateKeyVaultSecretId
$sslCert = Get-AzApplicationGatewaySslCertificate -Name $appGwSSLCertificateName -ApplicationGateway $gw
# Fetch public ip associated with Application Gateway:
$ipAddressResourceId = $gw.FrontendIPConfigurations.PublicIPAddress.Id
$ipAddressResource = Get-AzResource -ResourceId $ipAddressResourceId
$publicIp = Get-AzPublicIpAddress -ResourceGroupName $ipAddressResource.ResourceGroupName -Name $ipAddressResource.Name
$frontendIpConfig = $gw.FrontendIpConfigurations | Where-Object {$_.PublicIpAddress -ne $null}
$port = New-AzApplicationGatewayFrontendPort -Name "port_443" -Port 443
Add-AzApplicationGatewayFrontendPort -Name "port_443" -ApplicationGateway $gw -Port 443
Add-AzApplicationGatewayHttpListener -Name $httpListenerName -ApplicationGateway $gw -Protocol Https -FrontendIPConfiguration $frontendIpConfig -FrontendPort $port -SslCertificate $sslCert
# Update Application Gateway with the new HTTPS listener:
Set-AzApplicationGateway -ApplicationGateway $gw
多くの場合、ポート 80 を使う HTTP 用のパブリック リスナーは既に存在します。 次のスクリプトでは、そのようなものがまだない場合は作成されます。
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
$httpListenerName = "<name for the listener to add if not exists yet>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Check if HTTP listener on port 80 already exists:
$port = $gw.FrontendPorts | Where-Object {$_.Port -eq 80}
$listener = $gw.HttpListeners | Where-Object {$_.Protocol.ToString().ToLower() -eq "http" -and $_.FrontendPort.Id -eq $port.Id}
if ($listener -eq $null){
$frontendIpConfig = $gw.FrontendIpConfigurations | Where-Object {$_.PublicIpAddress -ne $null}
Add-AzApplicationGatewayHttpListener -Name $httpListenerName -ApplicationGateway $gw -Protocol Http -FrontendIPConfiguration $frontendIpConfig -FrontendPort $port
# Update Application Gateway with the new HTTPS listener:
Set-AzApplicationGateway -ApplicationGateway $gw
}
以前に構成したバックエンド プールと HTTP の設定を使用すると、リスナーからトラフィックを取得し、HTTP の設定を使ってバックエンド プールにそれをルーティングするよう、要求ルーティング規則を設定できます。 このためには、既存のルーティング規則にまだバインドされていない HTTP または HTTPS リスナーを使用できることを確認します。
- [規則] で、クリックして新しい "要求ルーティング規則" を追加します
- 規則の名前を指定します
- 既存のルーティング規則にまだバインドされていない HTTP または HTTPS リスナーを選びます
- [バックエンド ターゲット] で、App Service が構成されているバックエンド プールを選びます
- Application Gateway が App Service バックエンドへの接続に使用する必要がある HTTP 設定を構成します
- [追加] を選んでこの構成を保存します
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
$httpListenerName = "<name for existing http listener (without rule) to route traffic from>"
$httpSettingsName = "<name for http settings to use>"
$appGwBackendPoolNameForAppSvc = "<name for backend pool to route to>"
$reqRoutingRuleName = "<name for request routing rule to be added>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Get HTTP Settings:
$httpListener = Get-AzApplicationGatewayHttpListener -Name $httpListenerName -ApplicationGateway $gw
$httpSettings = Get-AzApplicationGatewayBackendHttpSettings -Name $httpSettingsName -ApplicationGateway $gw
$backendPool = Get-AzApplicationGatewayBackendAddressPool -Name $appGwBackendPoolNameForAppSvc -ApplicationGateway $gw
# Add routing rule:
Add-AzApplicationGatewayRequestRoutingRule -Name $reqRoutingRuleName -ApplicationGateway $gw -RuleType Basic -BackendHttpSettings $httpSettings -HttpListener $httpListener -BackendAddressPool $backendPool
# Update Application Gateway with the new routing rule:
Set-AzApplicationGateway -ApplicationGateway $gw
テスト
これを行う前に、バックエンドの正常性が正常と表示されていることを確認します。
[バックエンド正常性] セクションを開き、[状態] 列に HTTP 設定とバックエンド プールの組み合わせが "正常" と示されていることを確認します。
次に、Application Gateway の IP アドレスまたは IP アドレスに関連付けられている DNS 名のいずれかを使って、Web アプリケーションを参照します。 どちらも、Application Gateway の [概要] ページの [基本] のプロパティとして表示されます。 または、パブリック IP アドレス リソースにも、IP アドレスとそれに関連付けられている DNS 名が表示されます。
アプリケーションをテストするときに発生する可能性のある現象の一部を示した次の一覧に注意してください。
- Application Gateway ではなく ".azurewebsites.net" を直接指しているリダイレクト
- これには、".azurewebsites.net" に直接アクセスしようとする認証リダイレクトが含まれます
- バックエンドに渡されない、ドメインにバインドされた Cookie
- これには、App Service での "ARR アフィニティ" の設定の使用が含まれます
上記の状態 (アーキテクチャ センターで詳しく説明されています) は、Web アプリケーションでホスト名の書き換えが適切に処理されていないことを示します。 これはよく見られます。 これに対処する推奨される方法は、カスタム ドメインを使って App Service で Application Gateway を構成する手順に従うことです。 また、「Application Gateway での App Service に関する問題のトラブルシューティング」もご覧ください。
[バックエンド正常性] セクションを開き、[状態] 列に HTTP 設定とバックエンド プールの組み合わせが "正常" と示されていることを確認します。
次に、Application Gateway とバックエンドの App Service の両方に関連付けたカスタム ドメインを使って、Web アプリケーションを参照します。
バックエンドと HTTP 設定の正常性が "正常" と示されるかどうかを調べます。
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Check health:
Get-AzApplicationGatewayBackendHealth -ResourceGroupName $rgName -Name $appGwName
構成をテストするため、カスタム ドメインを使って Application Gateway 経由で App Service にコンテンツを要求します。
$customDomainName = "<FQDN for custom ___domain pointing to Application Gateway>"
Invoke-WebRequest $customDomainName
バックエンドと HTTP 設定の正常性が "正常" と示されるかどうかを調べます。
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Check health:
Get-AzApplicationGatewayBackendHealth -ResourceGroupName $rgName -Name $appGwName
構成をテストするため、IP アドレスを使って Application Gateway 経由で App Service にコンテンツを要求します。
$rgName = "<name of resource group for App Gateway>"
$appGwName = "<name of the App Gateway>"
# Get existing Application Gateway:
$gw = Get-AzApplicationGateway -Name $appGwName -ResourceGroupName $rgName
# Get ip address:
$ipAddressResourceId = $gw.FrontendIPConfigurations.PublicIPAddress.Id
$ipAddressResource = Get-AzResource -ResourceId $ipAddressResourceId
$publicIp = Get-AzPublicIpAddress -ResourceGroupName $ipAddressResource.ResourceGroupName -Name $ipAddressResource.Name
Write-Host "Public ip address for Application Gateway is $($publicIp.IpAddress)"
Invoke-WebRequest "http://$($publicIp.IpAddress)"
アプリケーションをテストするときに発生する可能性のある現象の一部を示した次の一覧に注意してください。
- Application Gateway ではなく ".azurewebsites.net" を直接指しているリダイレクト
- これには、".azurewebsites.net" に直接アクセスしようとする App Service の認証のリダイレクトが含まれます
- バックエンドに渡されない、ドメインにバインドされた Cookie
- これには、App Service での "ARR アフィニティ" の設定の使用が含まれます
上記の状態 (アーキテクチャ センターで詳しく説明されています) は、Web アプリケーションでホスト名の書き換えが適切に処理されていないことを示します。 これはよく見られます。 これに対処する推奨される方法は、カスタム ドメインを使って App Service で Application Gateway を構成する手順に従うことです。 また、「Application Gateway での App Service に関する問題のトラブルシューティング」もご覧ください。
アクセスの制限
これらの例でデプロイした Web アプリでは、インターネットから直接アクセスできるパブリック IP アドレスを使用します。 これは、新機能について学習するときや新しいことを試すときに、トラブルシューティングを行うのに役立ちます。 しかし、機能を運用環境にデプロイする場合は、制限を厳しくする必要があります。 次のオプションを検討してください。
- サービス エンドポインに基づくアクセス制限規則を構成します。 これにより、アプリへのインバウンド アクセスをロックして、Application Gateway を確実にソース アドレスにすることができます。
- Azure App Service の静的 IP の制限を使います。 たとえば、アプリケーション ゲートウェイからのトラフィックのみを受信するように Web アプリを制限できます。 アプリ サービスの IP 制限の機能を使用して、アクセス権を持つ唯一のアドレスとして、アプリケーション ゲートウェイの VIP を一覧表示します。