在 Azure 入口網站中設定 App Service 應用程式Configure an App Service app in the Azure portal

本文說明如何使用 Azure 入口網站來設定 web 應用程式、行動後端或 API 應用程式的一般設定。This article explains how to configure common settings for web apps, mobile back end, or API app using the Azure portal.

進行應用程式設定Configure app settings

在 App Service 中,應用程式設定是以環境變數形式傳遞至應用程式程式碼的變數。In App Service, app settings are variables passed as environment variables to the application code. 針對 Linux 應用程式和自訂容器,App Service 會使用旗標將應用程式設定傳遞至容器, --env 以設定容器中的環境變數。For Linux apps and custom containers, App Service passes app settings to the container using the --env flag to set the environment variable in the container.

Azure 入口網站中,搜尋並選取 [應用程式服務],然後選取您的應用程式。In the Azure portal, search for and select App Services, and then select your app.

搜尋應用程式服務

在應用程式的左側功能表中,選取 [ > 設定應用程式設定]。In the app's left menu, select Configuration > Application settings.

應用程式設定

針對 ASP.NET 和 ASP.NET Core 開發人員,在 App Service 中設定應用程式設定,就像是在 <appSettings> Web.configappsettings.js 中設定它們,但 App Service 中的值會覆寫 Web.configappsettings.js 中的值。For ASP.NET and ASP.NET Core developers, setting app settings in App Service are like setting them in <appSettings> in Web.config or appsettings.json, but the values in App Service override the ones in Web.config or appsettings.json. 您可以繼續進行開發設定 (例如, Web.config 中的本機 MySQL 密碼) 或 appsettings.js 和生產秘密 (例如,) 安全地 App Service 的 Azure MySQL 資料庫密碼。You can keep development settings (for example, local MySQL password) in Web.config or appsettings.json and production secrets (for example, Azure MySQL database password) safely in App Service. 當您在本機進行偵錯工具時,相同的程式碼會使用您的開發設定,並在部署至 Azure 時使用您的生產秘密。The same code uses your development settings when you debug locally, and it uses your production secrets when deployed to Azure.

同樣地,其他語言堆疊會在執行時間取得應用程式設定作為環境變數。Other language stacks, likewise, get the app settings as environment variables at runtime. 如需語言堆疊的特定步驟,請參閱:For language-stack specific steps, see:

應用程式設定在儲存時一律加密 (待用加密)。App settings are always encrypted when stored (encrypted-at-rest).

注意

您也可以使用Key Vault 參考,從Key Vault解析應用程式設定。App settings can also be resolved from Key Vault using Key Vault references.

顯示隱藏的值Show hidden values

根據預設,應用程式設定的值會在入口網站中隱藏,以獲得安全性。By default, values for app settings are hidden in the portal for security. 若要查看應用程式設定的隱藏值,請按一下該設定的 [ ] 欄位。To see a hidden value of an app setting, click the Value field of that setting. 若要查看所有應用程式設定的值,請按一下 [ 顯示值 ] 按鈕。To see the values of all app settings, click the Show value button.

新增或編輯Add or edit

若要加入新的應用程式設定,請按一下 [ 新增應用程式設定]。To add a new app setting, click New application setting. 在對話方塊中,您可以將設定放在 目前的位置。In the dialog, you can stick the setting to the current slot.

若要編輯設定,請按一下右側的 [ 編輯 ] 按鈕。To edit a setting, click the Edit button on the right side.

完成時,按一下 [ 更新]。When finished, click Update. 忘了按一下[設定] 頁面中的 [重新 回]。Don't forget to click Save back in the Configuration page.

注意

在預設的 Linux 容器或自訂 Linux 容器中,應用程式設定名稱中的任何嵌套 JSON 索引鍵結構都必須 ApplicationInsights:InstrumentationKey 在 App Service 中設定,以作為索引 ApplicationInsights__InstrumentationKey 鍵名稱。In a default Linux container or a custom Linux container, any nested JSON key structure in the app setting name like ApplicationInsights:InstrumentationKey needs to be configured in App Service as ApplicationInsights__InstrumentationKey for the key name. 換句話說,您 : 應該將任何 __ (雙底線) 取代。In other words, any : should be replaced by __ (double underscore).

大量編輯Edit in bulk

若要大量新增或編輯應用程式設定,請按一下 [ Advanced edit ] \ (編輯 \)按鈕。To add or edit app settings in bulk, click the Advanced edit button. 完成時,按一下 [ 更新]。When finished, click Update. 忘了按一下[設定] 頁面中的 [重新 回]。Don't forget to click Save back in the Configuration page.

應用程式設定具有下列 JSON 格式:App settings have the following JSON formatting:

[
  {
    "name": "<key-1>",
    "value": "<value-1>",
    "slotSetting": false
  },
  {
    "name": "<key-2>",
    "value": "<value-2>",
    "slotSetting": false
  },
  ...
]

使用 Azure CLI 將應用程式設定自動化Automate app settings with the Azure CLI

您可以使用 Azure CLI 從命令列建立和管理設定。You can use the Azure CLI to create and manage settings from the command line.

  • 使用 az webapp config 應用程式設定,將值指派給設定:Assign a value to a setting with az webapp config app settings set:

    az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings <setting-name>="<value>"
    

    <setting-name>請以設定的名稱取代,並以 <value> 要指派給它的值取代。Replace <setting-name> with the name of the setting, and <value> with the value to assign to it. 此命令會建立尚未存在的設定。This command creates the setting if it doesn't already exist.

  • 使用 az webapp config appsettings list來顯示所有設定及其值:Show all settings and their values with az webapp config appsettings list:

    az webapp config appsettings list --name <app-name> --resource-group <resource-group-name>
    
  • 移除一或多個設定,並使用 az webapp config app settings deleteRemove one or more settings with az webapp config app settings delete:

    az webapp config appsettings delete --name <app-name> --resource-group <resource-group-name> --setting-names {<names>}
    

    取代 <names> 為以空格分隔的設定名稱清單。Replace <names> with a space-separated list of setting names.

設定連接字串Configure connection strings

Azure 入口網站中,搜尋並選取 [應用程式服務],然後選取您的應用程式。In the Azure portal, search for and select App Services, and then select your app. 在應用程式的左側功能表中,選取 [ > 設定應用程式設定]。In the app's left menu, select Configuration > Application settings.

應用程式設定

若為 ASP.NET 和 ASP.NET Core 開發人員,在 App Service 中設定連接字串,就像在Web.config中設定連接字串 <connectionStrings> ,但您在 App Service 中設定的值會覆寫 Web.config 中的連接字串。 您可以保留開發設定 (例如,資料庫檔案) Web.config 和生產秘密 (例如,SQL Database 安全地) 認證。For ASP.NET and ASP.NET Core developers, setting connection strings in App Service are like setting them in <connectionStrings> in Web.config, but the values you set in App Service override the ones in Web.config. You can keep development settings (for example, a database file) in Web.config and production secrets (for example, SQL Database credentials) safely in App Service. 當您在本機進行偵錯工具時,相同的程式碼會使用您的開發設定,並在部署至 Azure 時使用您的生產秘密。The same code uses your development settings when you debug locally, and it uses your production secrets when deployed to Azure.

針對其他語言堆疊,最好改為使用 應用程式設定 ,因為連接字串需要變數索引鍵的特殊格式,才能存取這些值。For other language stacks, it's better to use app settings instead, because connection strings require special formatting in the variable keys in order to access the values.

注意

有一種情況下,您可能會想要使用連接字串,而不是 non-.NET 語言的應用程式設定:只有當您在 App Service 應用程式中設定資料庫的連接字串時, 會將某些 Azure 資料庫類型與應用程式一起備份。There is one case where you may want to use connection strings instead of app settings for non-.NET languages: certain Azure database types are backed up along with the app only if you configure a connection string for the database in your App Service app. 如需詳細資訊,請參閱已 備份的內容For more information, see What gets backed up. 如果您不需要此自動備份,請使用應用程式設定。If you don't need this automated backup, then use app settings.

在執行時間,連接字串會以環境變數的形式提供,並在前面加上下列連線類型:At runtime, connection strings are available as environment variables, prefixed with the following connection types:

  • Server SQLCONNSTR_SQLServer: SQLCONNSTR_
  • MySQL: MYSQLCONNSTR_MySQL: MYSQLCONNSTR_
  • SQLAzure SQLAZURECONNSTR_SQLAzure: SQLAZURECONNSTR_
  • 自訂:CUSTOMCONNSTR_Custom: CUSTOMCONNSTR_
  • 于 postgresql POSTGRESQLCONNSTR_PostgreSQL: POSTGRESQLCONNSTR_

例如,名為 connectionstring1 的 MySql 連接字串可以存取為環境變數 MYSQLCONNSTR_connectionString1For example, a MySql connection string named connectionstring1 can be accessed as the environment variable MYSQLCONNSTR_connectionString1. 如需語言堆疊的特定步驟,請參閱:For language-stack specific steps, see:

連接字串在儲存時一律加密 (待用加密)。Connection strings are always encrypted when stored (encrypted-at-rest).

注意

使用Key Vault 參考,也可以從Key Vault解析連接字串。Connection strings can also be resolved from Key Vault using Key Vault references.

顯示隱藏的值Show hidden values

根據預設,連接字串的值會在入口網站中隱藏,以獲得安全性。By default, values for connection strings are hidden in the portal for security. 若要查看連接字串的隱藏值,只需按一下該字串的 欄位。To see a hidden value of a connection string, just click the Value field of that string. 若要查看所有連接字串的值,請按一下 [ 顯示值 ] 按鈕。To see the values of all connection strings, click the Show value button.

新增或編輯Add or edit

若要加入新的連接字串,請按一下 [ 新增連接字串]。To add a new connection string, click New connection string. 在對話方塊中,您可以將 連接字串放到目前的位置。In the dialog, you can stick the connection string to the current slot.

若要編輯設定,請按一下右側的 [ 編輯 ] 按鈕。To edit a setting, click the Edit button on the right side.

完成時,按一下 [ 更新]。When finished, click Update. 忘了按一下[設定] 頁面中的 [重新 回]。Don't forget to click Save back in the Configuration page.

大量編輯Edit in bulk

若要大量加入或編輯連接字串,請按一下 [ Advanced edit ] 按鈕。To add or edit connection strings in bulk, click the Advanced edit button. 完成時,按一下 [ 更新]。When finished, click Update. 忘了按一下[設定] 頁面中的 [重新 回]。Don't forget to click Save back in the Configuration page.

連接字串具有下列 JSON 格式:Connection strings have the following JSON formatting:

[
  {
    "name": "name-1",
    "value": "conn-string-1",
    "type": "SQLServer",
    "slotSetting": false
  },
  {
    "name": "name-2",
    "value": "conn-string-2",
    "type": "PostgreSQL",
    "slotSetting": false
  },
  ...
]

設定一般設定Configure general settings

Azure 入口網站中,搜尋並選取 [應用程式服務],然後選取您的應用程式。In the Azure portal, search for and select App Services, and then select your app. 在應用程式的左側功能表中,選取 > [設定一般設定]。In the app's left menu, select Configuration > General settings.

一般設定

在這裡,您可以設定應用程式的一些常見設定。Here, you can configure some common settings for the app. 某些設定需要您 擴大至更高的定價層Some settings require you to scale up to higher pricing tiers.

  • 堆疊設定:用來執行應用程式的軟體堆疊,包括語言和 SDK 版本。Stack settings: The software stack to run the app, including the language and SDK versions.

    針對 Linux 應用程式和自訂容器應用程式,您可以選取語言執行時間版本,並設定選用的 啟動命令 或啟動命令檔。For Linux apps and custom container apps, you can select the language runtime version and set an optional Startup command or a startup command file.

    Linux 容器的一般設定

  • 平臺設定:可讓您設定裝載平臺的設定,包括:Platform settings: Lets you configure settings for the hosting platform, including:

    • 位數:32位或64位。Bitness: 32-bit or 64-bit.
    • WebSocket 通訊協定:例如,針對 ASP.NET SignalRsocket.ioWebSocket protocol: For ASP.NET SignalR or socket.io, for example.
    • Always On:即使沒有流量,仍會讓應用程式保持載入。Always On: Keeps the app loaded even when there's no traffic. 連續 Webjob 或使用 CRON 運算式觸發的 Webjob 都需要它。It's required for continuous WebJobs or for WebJobs that are triggered using a CRON expression.

      注意

      使用 Always On 功能時,前端負載平衡器會將要求傳送至應用程式根目錄。With the Always On feature, the front end load balancer sends a request to the application root. 無法設定 App Service 的應用程式端點。This application endpoint of the App Service can't be configured.

    • 受控管線版本: IIS [管線模式]。Managed pipeline version: The IIS [pipeline mode]. 如果您有需要舊版 IIS 的繼承應用程式,請將它設定為 [ 傳統 ]。Set it to Classic if you have a legacy app that requires an older version of IIS.
    • HTTP 版本:設為 2.0 ,以啟用對 HTTPS/2 通訊協定的支援。HTTP version: Set to 2.0 to enable support for HTTPS/2 protocol.

    注意

    大部分的新式瀏覽器僅支援透過 TLS 的 HTTP/2 通訊協定,非加密的流量則會繼續使用 HTTP/1.1。Most modern browsers support HTTP/2 protocol over TLS only, while non-encrypted traffic continues to use HTTP/1.1. 為確保用戶端瀏覽器會使用 HTTP/2 連接到您的應用程式,請保護您的自訂 DNS 名稱。To ensure that client browsers connect to your app with HTTP/2, secure your custom DNS name. 如需詳細資訊,請參閱 Azure App Service 中的使用 TLS/SSL 系結保護自訂 DNS 名稱For more information, see Secure a custom DNS name with a TLS/SSL binding in Azure App Service.

    • ARR 親和性:在多重實例部署中,確定用戶端會在會話的存留期間路由傳送至相同的實例。ARR affinity: In a multi-instance deployment, ensure that the client is routed to the same instance for the life of the session. 您可以針對無狀態應用程式將此選項設定為 [ 關閉 ]。You can set this option to Off for stateless applications.
  • 調試 程式:為 ASP.NETASP.NET CoreNode.js 應用程式啟用遠端偵錯程式。Debugging: Enable remote debugging for ASP.NET, ASP.NET Core, or Node.js apps. 此選項會在48小時後自動關閉。This option turns off automatically after 48 hours.

  • 傳入用戶端憑證:需要 雙向驗證的用戶端憑證。Incoming client certificates: require client certificates in mutual authentication.

設定預設檔Configure default documents

這項設定僅適用于 Windows 應用程式。This setting is only for Windows apps.

Azure 入口網站中,搜尋並選取 [應用程式服務],然後選取您的應用程式。In the Azure portal, search for and select App Services, and then select your app. 在應用程式的左側功能表中,選取 [ 設定 > 預設檔]。In the app's left menu, select Configuration > Default documents.

預設文件

預設檔是顯示在網站根 URL 的網頁。The default document is the web page that's displayed at the root URL for a website. 系統會使用清單中第一個相符的檔案。The first matching file in the list is used. 若要加入新的預設檔,請按一下 [ 新增檔]。To add a new default document, click New document. 別忘了按一下 [ 儲存]。Don't forget to click Save.

如果應用程式使用以 URL 為基礎的模組,而不是提供靜態內容,則不需要預設檔。If the app uses modules that route based on URL instead of serving static content, there is no need for default documents.

設定路徑對應Configure path mappings

Azure 入口網站中,搜尋並選取 [應用程式服務],然後選取您的應用程式。In the Azure portal, search for and select App Services, and then select your app. 在應用程式的左側功能表中,選取 [ 設定 > 路徑 對應]。In the app's left menu, select Configuration > Path mappings.

路徑對應

注意

[ 路徑 對應] 索引標籤可能會顯示作業系統特定的設定,這些設定與此處所示的範例不同。The Path mappings tab may display OS-specific settings that differ from the example shown here.

Windows 應用程式 (uncontainerized) Windows apps (uncontainerized)

針對 Windows 應用程式,您可以自訂 IIS 處理常式對應和虛擬應用程式和目錄。For Windows apps, you can customize the IIS handler mappings and virtual applications and directories.

處理常式對應可讓您新增自訂腳本處理器以處理特定副檔名的要求。Handler mappings let you add custom script processors to handle requests for specific file extensions. 若要加入自訂處理常式,請按一下 [ 新增處理常式對應]。To add a custom handler, click New handler mapping. 設定處理常式,如下所示:Configure the handler as follows:

  • 延伸 模組。Extension. 您要處理的副檔名,例如 * phphandler.fcgiThe file extension you want to handle, such as *.php or handler.fcgi.
  • 腳本處理器Script processor. 腳本處理器的絕對路徑。The absolute path of the script processor to you. 符合副檔名之檔案的要求會由腳本處理器處理。Requests to files that match the file extension are processed by the script processor. 使用路徑 D:\home\site\wwwroot 以指出應用程式的根目錄。Use the path D:\home\site\wwwroot to refer to your app's root directory.
  • 引數Arguments. 腳本處理器的選擇性命令列引數。Optional command-line arguments for the script processor.

每個應用程式都有預設的根路徑 (/) 對應至 D:\home\site\wwwroot ,預設會部署您的程式碼。Each app has the default root path (/) mapped to D:\home\site\wwwroot, where your code is deployed by default. 如果您的應用程式根目錄位於不同的資料夾中,或您的儲存機制有多個應用程式,您可以在這裡編輯或新增虛擬應用程式和目錄。If your app root is in a different folder, or if your repository has more than one application, you can edit or add virtual applications and directories here.

在 [ 路徑 對應] 索引標籤中,按一下 [ 新增虛擬應用程式或目錄]。From the Path mappings tab, click New virtual application or directory.

  • 若要將虛擬目錄對應至實體路徑,請將 [ 目錄 ] 核取方塊保留為已選取。To map a virtual directory to a physical path, leave the Directory check box selected. 指定虛擬目錄,以及對應至網站根目錄 () 的相對 (實體) 路徑 D:\homeSpecify the virtual directory and the corresponding relative (physical) path to the website root (D:\home).

  • 若要將虛擬目錄標示為 web 應用程式,請清除 [ 目錄 ] 核取方塊。To mark a virtual directory as a web application, clear the Directory check box.

    目錄核取方塊

容器化應用程式Containerized apps

您可以 為容器化應用程式新增自訂儲存體You can add custom storage for your containerized app. 容器化應用程式包含所有 Linux 應用程式,以及在 App Service 上執行的 Windows 和 Linux 自訂容器。Containerized apps include all Linux apps and also the Windows and Linux custom containers running on App Service. 按一下 [ 新增] Azure 儲存體掛接 並設定您的自訂存放裝置,如下所示:Click New Azure Storage Mount and configure your custom storage as follows:

  • 名稱:顯示名稱。Name: The display name.
  • 設定 選項基本AdvancedConfiguration options: Basic or Advanced.
  • 儲存體帳戶:具有您想要之容器的儲存體帳戶。Storage accounts: The storage account with the container you want.
  • 儲存體類型Azure blobAzure 檔案儲存體Storage type: Azure Blobs or Azure Files.

    注意

    Windows 容器應用程式僅支援 Azure 檔案儲存體。Windows container apps only support Azure Files.

  • 儲存體容器:適用于基本設定、您要的容器。Storage container: For basic configuration, the container you want.
  • 共用名稱:用於 advanced configuration,檔案共用名稱。Share name: For advanced configuration, the file share name.
  • 存取金鑰:適用于 advanced configuration,存取金鑰。Access key: For advanced configuration, the access key.
  • 掛接路徑:容器中用來掛接自訂存放裝置的絕對路徑。Mount path: The absolute path in your container to mount the custom storage.

如需詳細資訊,請參閱 從 App Service 中的容器存取 Azure 儲存體做為網路共用For more information, see Access Azure Storage as a network share from a container in App Service.

設定語言堆疊設定Configure language stack settings

設定自訂容器Configure custom containers

請參閱 為 Azure App Service 設定自訂 Linux 容器See Configure a custom Linux container for Azure App Service

下一步Next steps

[在 Azure App Service 中設定預備環境]: ./deploy-staging-slots.md (部分機器翻譯) [Set up staging environments in Azure App Service]: ./deploy-staging-slots.md [How to: Monitor web endpoint status]: ./web-sites-monitor.md [在 Azure App Service 中監視基本概念]: ./web-sites-monitor.md [Monitoring basics in Azure App Service]: ./web-sites-monitor.md [管線模式]: https://www.iis.net/learn/get-started/introduction-to-iis/introduction-to-iis-architecture#Application [pipeline mode]: https://www.iis.net/learn/get-started/introduction-to-iis/introduction-to-iis-architecture#Application [在 Azure App Service 中調整應用程式規模]: ./manage-scale-up.md [Scale an app in Azure App Service]: ./manage-scale-up.md