在 Azure 门户中配置应用服务应用Configure an App Service app in the Azure portal

本主题介绍如何使用 Azure 门户配置 Web 应用、移动后端或 API 应用的常用设置。This topic explains how to configure common settings for web apps, mobile back end, or API app using the Azure portal.

配置应用设置Configure app settings

在应用服务中,应用设置是作为环境变量传递给应用程序代码的变量。In App Service, app settings are variables passed as environment variables to the application code. 对于 Linux 应用和自定义容器,应用服务使用 --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 开发人员而言,在应用服务中设置应用设置类似于在 Web.config 或 appsettings.json 中的 <appSettings> 内进行设置,但应用服务中的值会替代 Web.config 或 appsettings.json 中的值。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 或 appsettings.json 中保留开发设置(例如,本地 MySQL 密码),但在应用服务中保留生产机密(例如 Azure MySQL 数据库密码)会更安全。You can keep development settings (for example, local MySQL password) in Web.config or appsettings.json, but production secrets (for example, Azure MySQL database password) safe 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 容器中,需要在应用服务中将应用设置名称(如 ApplicationInsights:InstrumentationKey)中的任何嵌套 JSON 密钥结构配置为密钥名称的 ApplicationInsights__InstrumentationKeyIn 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

若要批量添加或编辑应用设置,请单击“高级编辑”按钮。 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
  },
  ...
]

配置连接字符串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 开发人员而言,在应用服务中设置连接字符串类似于在 Web.config 中的 <connectionStrings> 内进行设置,但应用服务中设置的值会替代 Web.config 中的值。可将开发设置(例如,数据库文件)保留在 Web.config 中,并将生产机密(例如,SQL 数据库凭据)安全保留在应用服务中。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. 但以下情况例外:如果在应用中配置了相应的连接字符串,则某些 Azure 数据库类型会连同应用一起备份。Here's one exception, however: certain Azure database types are backed up along with the app if you configure their connection strings in your 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:

  • SQLServer:SQLCONNSTR_SQLServer: SQLCONNSTR_
  • MySQL: MYSQLCONNSTR_MySQL: MYSQLCONNSTR_
  • SQLAzure:SQLAZURECONNSTR_SQLAzure: SQLAZURECONNSTR_
  • 自定义:CUSTOMCONNSTR_Custom: CUSTOMCONNSTR_
  • PostgreSQL:POSTGRESQLCONNSTR_PostgreSQL: POSTGRESQLCONNSTR_

例如,可以使用环境变量 MYSQLCONNSTR_connectionString1 的形式访问名为 connectionstring1 的 MySql 连接字符串。For 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

若要批量添加或编辑连接字符串,请单击“高级编辑”按钮。 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 also set an optional start-up command or file.
  • 平台设置:用于配置托管平台的设置,包括: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. 对于连续性 WebJobs 或使用 CRON 表达式触发的 WebJobs,它是必需的。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. 无法配置应用服务的此应用程序终结点。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 应用服务中使用 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.

路径映射

“路径映射”页根据 OS 类型显示不同的内容。 The Path mappings page shows you different things based on the OS type.

Windows 应用(未容器化)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. 按如下所述配置处理程序: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. 单击“新建虚拟应用程序或目录”。 Click New virtual application or directory.

若要配置虚拟应用程序和目录,请指定每个虚拟目录及其相对于网站根目录 (D:\home) 的物理路径。To configure virtual applications and directories, specify each virtual directory and its corresponding physical path relative to the website root (D:\home). 还可选中“应用程序” 复选框,将虚拟目录标记为应用程序。Optionally, you can select the Application checkbox to mark a virtual directory as an application.

容器化应用Containerized apps

单击“新 Azure 存储装载”,然后按如下所示配置自定义存储:Click New Azure Storage Mount and configure your custom storage as follows:

  • 名称:显示名称。Name: The display name.
  • 配置选项:“基本”或“高级”。Configuration options: Basic or Advanced.
  • 存储帐户:具有所需容器的存储帐户。Storage accounts: The storage account with the container you want.
  • 存储类型:“Azure Blob”或“Azure 文件存储”。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.
  • 共享名:对于高级配置,为文件共享名。Share name: For advanced configuration, the file share name.
  • 访问密钥:对于高级配置,为访问密钥。Access key: For advanced configuration, the access key.
  • 装载路径:容器中用于装载自定义存储的绝对路径。Mount path: The absolute path in your container to mount the custom storage.

配置语言堆栈设置Configure language stack settings

对于 Linux 应用,请参阅:For Linux apps, see:

后续步骤Next steps