--- lang: ja-jp breaks: true --- # Azure App Server Webアプリが使用するポートにhttpsリクエストを転送する設定 2021-05-07 #### Visual Studio Codeから直接デプロイした場合の設定であり、Dockerでデプロイする場合は関係ない。 > ### Azure App Service on Linux の FAQ > カスタム コンテナーがポート 80 以外のポートをリッスンしています。そのポートに要求をルーティングするようにアプリを構成するにはどうすればよいですか。 > ポートの自動検出機能があります。 また、WEBSITES_PORT というアプリ設定を指定して、必要なポート番号の値を設定することもできます。 以前、プラットフォームでは PORT アプリ設定を使用していました。 このアプリ設定を廃止し、WEBSITES_PORT のみを使用する予定です。 > https://docs.microsoft.com/ja-jp/azure/app-service/faq-app-service-linux > ### Azure App Service のカスタム コンテナーを構成する > ポート番号を構成する 既定では、App Service はカスタム コンテナーがポート 80 でリッスンしていることを前提としています。 コンテナーが別のポートをリッスンしている場合は、App Service アプリで WEBSITES_PORT アプリ設定を指定します。 これは、Cloud Shell を使用して設定できます。 Bash では次のとおりです > ```= > az webapp config appsettings set --resource-group <group-name> --name <app-name> --settings WEBSITES_PORT=8000 > ``` > https://docs.microsoft.com/ja-jp/azure/app-service/configure-custom-container?pivots=container-linux ## Azure App Server は、Webアプリが「8080」番ポートを利用する場合は、正常にリクエストが転送されるが、それ以外だとエラーとなる。 エラーとなるコード。3000番ポートで待ち受けている ```javascript= 'use strict'; const express = require('express'); // Constants //const PORT = 8080; const PORT = 3000; const HOST = '0.0.0.0'; // App const app = express(); app.get('/', (req, res) => { res.setHeader('Content-Type', 'text/plain; charset=utf-8'); //res.send('Hello World\n'); res.send('今日は世界!!\n'); }); app.listen(PORT, HOST); console.log(`Running on http://${HOST}:${PORT}`); ``` エラーログ ```= [1] 2021-05-07T01:09:49 Container appsvcportftest_1_294fd4f0 for site appsvcportftest did not start within expected time limit. [2] 2021-05-07T01:09:49 Container appsvcportftest_1_294fd4f0 didn't respond to HTTP pings on port: 8080, failing site start ``` ![](https://i.imgur.com/ALtnOkh.png) ## 3000番ポートが利用できるようにApp Serviceを構成する。 Azure CLIで実行。以下を実行したが正常に動作しなかった。 ```shell= $ az webapp config appsettings set --resource-group appsvsportftest_resource_group --name AppSvcPortFTest --settings WEBSITES_PORT=3000 [ { "name": "WEBSITES_PORT", "slotSetting": false, "value": "3000" } ] ``` ## 3000番ポートが利用できるようにApp Serviceを構成する。 Azure CLIで実行。以下設定を行うことで正常に動作を確認。 ```shell= $ $ az webapp config appsettings set --resource-group appsvsportftest_resource_group --name AppSvcPortFTest --settings PORT=3000 [ { "name": "WEBSITES_PORT", "slotSetting": false, "value": "3000" }, { "name": "PORT", "slotSetting": false, "value": "3000" } ] ``` ## 画面から設定する場合 ![](https://i.imgur.com/hKe7AEQ.png) ## 実行結果 ![](https://i.imgur.com/jaju7Qz.png) ###### tags: `Azure` `App Service`