Azure 中 Web 应用的应用程序性能常见问题解答Application performance FAQs for Web Apps in Azure

本文对 Azure 应用服务 Web 应用功能的应用程序性能常见问题 (FAQ) 进行了解答。This article has answers to frequently asked questions (FAQs) about application performance issues for the Web Apps feature of Azure App Service.

如果本文未解决你的 Azure 问题,请访问 MSDN 和 CSDN 上的 Azure 论坛。If your Azure issue is not addressed in this article, visit the Azure forums on MSDN and CSDN. 可以在这些论坛上发布问题。You can post your issue in these forums. 还可提交 Azure 支持请求。You also can submit an Azure support request. 若要提交支持请求,请在 Azure 支持页上提交。To submit a support request, on the Azure support page.

为何我的应用运行速度缓慢?Why is my app slow?

有多种因素可能导致应用性能降低。Multiple factors might contribute to slow app performance. 有关问题排查的详细步骤,请参阅 Troubleshoot slow web app performance(排查导致 Web 应用性能降低的问题)。For detailed troubleshooting steps, see Troubleshoot slow web app performance.

如何解决 CPU 占用高的问题?How do I troubleshoot a high CPU-consumption scenario?

在某些 CPU 占用高的情况下,应用可能真的需要更多计算资源。In some high CPU-consumption scenarios, your app might truly require more computing resources. 在这种情况下,请考虑缩放到更高的服务层,以便应用程序获取所需的所有资源。 In that case, consider scaling to a higher service tier so the application gets all the resources it needs. 其他情况下,高 CPU 占用可能是由错误循环或编码实践导致的。Other times, high CPU consumption might be caused by a bad loop or by a coding practice. 深入了解 CPU 占用升高的触发因素这一过程分为两部分。Getting insight into what's triggering increased CPU consumption is a two-part process. 首先,创建一个进程转储,然后分析该进程转储。First, create a process dump, and then analyze the process dump. 有关详细信息,请参阅捕获和分析 Web 应用高 CPU 占用的转储文件For more information, see Capture and analyze a dump file for high CPU consumption for Web Apps.

如何解决内存占用高的问题?How do I troubleshoot a high memory-consumption scenario?

在某些内存占用高的情况下,应用可能真的需要更多计算资源。In some high memory-consumption scenarios, your app might truly require more computing resources. 在这种情况下,请考虑缩放到更高的服务层,以便应用程序获取所需的所有资源。 In that case, consider scaling to a higher service tier so the application gets all the resources it needs. 在其他情况下,代码中存在的 bug 可能会导致内存泄漏。Other times, a bug in the code might cause a memory leak. 此外,编码实践也可能会增大内存占用。A coding practice also might increase memory consumption. 深入了解内存占用高的触发因素这一过程分为两部分。 Getting insight into what's triggering high memory consumption is a two-part process. 首先,创建进程转储,然后分析此进程转储。First, create a process dump, and then analyze the process dump. Azure 站点扩展库中的故障诊断程序可高效执行这两个步骤。Crash Diagnoser from the Azure Site Extension Gallery can efficiently perform both these steps. 有关详细信息,请参阅捕获和分析 Web 应用间歇性高内存的转储文件For more information, see Capture and analyze a dump file for intermittent high memory for Web Apps.

如何使用 PowerShell 实现应用服务 Web 应用的自动化?How do I automate App Service web apps by using PowerShell?

可以使用 PowerShell cmdlet 管理和维护应用服务 Web 应用。You can use PowerShell cmdlets to manage and maintain App Service web apps. 在我们的博客文章 Automate web apps hosted in Azure App Service by using PowerShell(使用 PowerShell 实现 Azure 应用服务中托管的 Web 应用的自动化)中,我们将说明如何使用基于 Azure Resource Manager 的 PowerShell cmdlet 自动执行常见任务。In our blog post Automate web apps hosted in Azure App Service by using PowerShell, we describe how to use Azure Resource Manager-based PowerShell cmdlets to automate common tasks. 此博客文章中还包含适用于各种 Web 应用管理任务的示例代码。The blog post also has sample code for various web apps management tasks. 有关所有应用服务 Web 应用 cmdlet 的说明和语法,请参阅 Az.WebsitesFor descriptions and syntax for all App Service web apps cmdlets, see Az.Websites.

如何查看 Web 应用的事件日志?How do I view my web app's event logs?

查看 Web 应用的事件日志:To view your web app's event logs:

  1. 登录到 Kudu 网站Sign in to your Kudu website.
  2. 在菜单中,选择“调试控制台” > “CMD”。In the menu, select Debug Console > CMD.
  3. 选择“LogFiles”文件夹。Select the LogFiles folder.
  4. 若要查看事件日志,请选择“eventlog.xml”旁边的铅笔图标。To view event logs, select the pencil icon next to eventlog.xml.
  5. 若要下载日志,请运行 PowerShell cmdlet Save-AzureWebSiteLog -Name webappnameTo download the logs, run the PowerShell cmdlet Save-AzureWebSiteLog -Name webappname.

如何捕获 Web 应用的用户模式内存转储?How do I capture a user-mode memory dump of my web app?

捕获 Web 应用的用户模式内存转储:To capture a user-mode memory dump of your web app:

  1. 登录到 Kudu 网站Sign in to your Kudu website.
  2. 选择“进程资源管理器”菜单。Select the Process Explorer menu.
  3. 右键单击“w3wp.exe”进程或 WebJob 进程。Right-click the w3wp.exe process or your WebJob process.
  4. 选择“下载内存转储” > “完全转储”。Select Download Memory Dump > Full Dump.

如何查看 Web 应用的进程级信息?How do I view process-level info for my web app?

可通过两种方法查看 Web 应用的进程级信息:You have two options for viewing process-level information for your web app:

  • 在 Azure 门户中:In the Azure portal:
    1. 打开 Web 应用的“进程资源管理器”。Open the Process Explorer for the web app.
    2. 若要查看详细信息,请选择“w3wp.exe”进程。To see the details, select the w3wp.exe process.
  • 在 Kudu 控制台中:In the Kudu console:
    1. 登录到 Kudu 网站Sign in to your Kudu website.
    2. 选择“进程资源管理器”菜单。Select the Process Explorer menu.
    3. 对于“w3wp.exe”进程,选择“属性”。For the w3wp.exe process, select Properties.

浏览到应用时,看到“错误 403 - 此 web 应用已停止。”When I browse to my app, I see "Error 403 - This web app is stopped." 如何解决此问题?How do I resolve this?

有三种情况可能导致此错误:Three conditions can cause this error:

  • Web 应用已达到计费限制,站点已被禁用。The web app has reached a billing limit and your site has been disabled.
  • 已在门户中停止 Web 应用。The web app has been stopped in the portal.
  • Web 应用已达到资源配额限制,该限制可能适用于免费或共享缩放服务计划。The web app has reached a resource quota limit that might apply to a Free or Shared scale service plan.

若要了解导致错误的原因并解决问题,请按照 Web 应用:“错误 403 - 此 Web 应用已停止”中的步骤执行。To see what is causing the error and to resolve the issue, follow the steps in Web Apps: "Error 403 – This web app is stopped".

可从何处了解有关各种应用服务计划的配额和限制的详细信息?Where can I learn more about quotas and limits for various App Service plans?

有关配额和限制的信息,请参阅应用服务限制For information about quotas and limits, see App Service limits.

如何缩短空闲时间后第一个请求的响应时间?How do I decrease the response time for the first request after idle time?

默认情况下,如果 Web 应用已处于空闲状态相当一段时间,则其处于未加载的状态。By default, web apps are unloaded if they are idle for a set period of time. 这样,系统可以节省资源。This way, the system can conserve resources. 其缺点是:Web 应用处于未加载的状态后,对第一个请求的响应时间较长,需要等待 Web 应用加载和启动处理响应。The downside is that the response to the first request after the web app is unloaded is longer, to allow the web app to load and start serving responses. 在基本和标准服务计划中,可启用“始终打开”设置,使应用保持加载状态。In Basic and Standard service plans, you can turn on the Always On setting to keep the app always loaded. 这样就无需在应用处于空闲状态后重新加载应用。This eliminates longer load times after the app is idle. 若要更改“始终打开”设置,请执行以下操作:To change the Always On setting:

  1. 在 Azure 门户中,转到自己的 Web 应用。In the Azure portal, go to your web app.
  2. 选择“应用程序设置”。Select Application settings.
  3. 对于“始终打开”,选择“打开”。For Always On, select On.

如何打开失败请求跟踪?How do I turn on failed request tracing?

若要打开失败的请求跟踪,请执行以下操作:To turn on failed request tracing:

  1. 在 Azure 门户中,转到自己的 Web 应用。In the Azure portal, go to your web app.

  2. 选择“所有设置” > “诊断日志”。Select All Settings > Diagnostics Logs.

  3. 对于“失败的请求跟踪”,选择“打开”。For Failed Request Tracing, select On.

  4. 选择“其他安全性验证” 。Select Save.

  5. 在 Web 应用边栏选项卡,选择“工具”。On the web app blade, select Tools.

  6. 选择“Visual Studio Online”。Select Visual Studio Online.

  7. 如果设置不是“打开”,则选择“打开”。If the setting is not On, select On.

  8. 选择“转到”。Select Go.

  9. 选择 Web.configSelect Web.config.

  10. 在 system.webServer 中,添加此配置(以捕获特定的 URL):In system.webServer, add this configuration (to capture a specific URL):

    <system.webServer>
    <tracing> <traceFailedRequests>
    <remove path="*api*" />
    <add path="*api*">
    <traceAreas>
    <add provider="ASP" verbosity="Verbose" />
    <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" />
    <add provider="ISAPI Extension" verbosity="Verbose" />
    <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression, Cache,RequestNotifications,Module,FastCGI" verbosity="Verbose" />
    </traceAreas>
    <failureDefinitions statusCodes="200-999" />
    </add> </traceFailedRequests>
    </tracing>
    
  11. 若要解决性能较低的问题,请添加此配置(如果捕获请求话费的时间超过 30 秒):To troubleshoot slow-performance issues, add this configuration (if the capturing request is taking more than 30 seconds):

    <system.webServer>
    <tracing> <traceFailedRequests>
    <remove path="*" />
    <add path="*">
    <traceAreas> <add provider="ASP" verbosity="Verbose" />
    <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Verbose" />
    <add provider="ISAPI Extension" verbosity="Verbose" />
    <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression, Cache,RequestNotifications,Module,FastCGI" verbosity="Verbose" />
    </traceAreas>
    <failureDefinitions timeTaken="00:00:30" statusCodes="200-999" />
    </add> </traceFailedRequests>
    </tracing>
    
  12. 若要下载失败的请求跟踪,请在门户中转到你的网站。To download the failed request traces, in the portal, go to your website.

  13. 选择“工具” > “Kudu” > “转到”。Select Tools > Kudu > Go.

  14. 在菜单中,选择“调试控制台” > “CMD”。In the menu, select Debug Console > CMD.

  15. 选择“LogFiles”文件夹,然后选择名称以“W3SVC”开头的文件夹。Select the LogFiles folder, and then select the folder with a name that starts with W3SVC.

  16. 若要查看 XML 文件,请选择铅笔图标。To see the XML file, select the pencil icon.

看到消息“由于‘内存百分比’限制工作进程请求回收。”I see the message "Worker Process requested recycle due to 'Percent Memory' limit." 如何解决此问题?How do I address this issue?

对于 32 位的进程(即使是在 64 位操作系统中),最大可用内存量是 2 GB。The maximum available amount of memory for a 32-bit process (even on a 64-bit operating system) is 2 GB. 默认情况下,应用服务中的工作进程设置为 32 位(以便与旧版 Web 应用程序兼容)。By default, the worker process is set to 32-bit in App Service (for compatibility with legacy web applications).

请考虑切换为 64 位进程,以便利用 Web 辅助角色中的其他可用内存。Consider switching to 64-bit processes so you can take advantage of the additional memory available in your Web Worker role. 这将触发 Web 应用重启,因此请进行相应安排。This triggers a web app restart, so schedule accordingly.

另请注意,64 位环境需要“基本”或“标准”服务计划。Also note that a 64-bit environment requires a Basic or Standard service plan. “免费”和“共享”计划始终在 32 位环境中运行。Free and Shared plans always run in a 32-bit environment.

有关详细信息,请参阅在应用服务中配置 web 应用For more information, see Configure web apps in App Service.

为何我的请求在 230 秒后超时?Why does my request time out after 230 seconds?

Azure 负载均衡器的默认空闲超时设置为四分钟。Azure Load Balancer has a default idle timeout setting of four minutes. 这通常是 Web 请求合理的响应时间限制。This is generally a reasonable response time limit for a web request. 如果 Web 应用需要后台处理,建议使用 Azure WebJobs。If your web app requires background processing, we recommend using Azure WebJobs. Azure Web 应用可以调用 WebJobs,并在后台处理完成时收到通知。The Azure web app can call WebJobs and be notified when background processing is finished. 有多种方法可用于使用 WebJobs,包括队列和触发器。You can choose from multiple methods for using WebJobs, including queues and triggers.

WebJobs 专用于后台处理。WebJobs is designed for background processing. 可在 WebJobs 中执行任意数量的后台处理。You can do as much background processing as you want in a WebJob. 有关 WebJobs 的详细信息,请参阅使用 WebJobs 运行后台任务For more information about WebJobs, see Run background tasks with WebJobs.

应用服务中托管的 ASP.NET Core 应用程序有时会停止响应。ASP.NET Core applications that are hosted in App Service sometimes stop responding. 如何解决此问题?How do I fix this issue?

早期 Kestrel 版本的已知问题可能会导致托管于应用服务中的 ASP.NET Core 1.0 应用间歇性地停止响应。A known issue with an earlier Kestrel version might cause an ASP.NET Core 1.0 app that's hosted in App Service to intermittently stop responding. 还可能会看到以下消息:“指定的 CGI 应用程序遇到错误,服务器终止了该进程”。You also might see this message: "The specified CGI Application encountered an error and the server terminated the process."

已在 Kestrel 版本 1.0.2 中修复了此问题。This issue is fixed in Kestrel version 1.0.2. 此版本包含在 ASP.NET Core 1.0.3 更新中。This version is included in the ASP.NET Core 1.0.3 update. 若要解决此问题,请确保将你的应用依赖项更新为使用 Kestrel 1.0.2。To resolve this issue, make sure you update your app dependencies to use Kestrel 1.0.2. 或者,可以使用博客文章 ASP.NET Core 1.0 slow perf issues in App Service web apps(应用服务 Web 应用中 ASP.NET Core 1.0 低性能问题)中介绍的两种解决方法之一。Alternatively, you can use one of two workarounds that are described in the blog post ASP.NET Core 1.0 slow perf issues in App Service web apps.

在 Web 应用的文件结构中找不到日志文件。I can't find my log files in the file structure of my web app. 如何找到它们?How can I find them?

如果使用应用服务的本地缓存功能,应用服务实例“LogFiles 和数据”文件夹的文件夹结构会受到影响。If you use the Local Cache feature of App Service, the folder structure of the LogFiles and Data folders for your App Service instance are affected. 使用本地缓存时,将在存储 LogFiles 和数据文件夹中创建子文件夹。When Local Cache is used, subfolders are created in the storage LogFiles and Data folders. 子文件夹使用“唯一标识符”+ 时间戳的命名模式。The subfolders use the naming pattern "unique identifier" + time stamp. 每个子文件夹对应于一个 VM 实例,其中的 Web 应用正在运行或已运行。Each subfolder corresponds to a VM instance in which the web app is running or has run.

若要确定是否是在本地缓存,请检查应用服务的“应用程序设置”选项卡。如果在使用本地缓存,应用设置 WEBSITE_LOCAL_CACHE_OPTION 设置为 AlwaysTo determine whether you are using Local Cache, check your App Service Application settings tab. If Local Cache is being used, the app setting WEBSITE_LOCAL_CACHE_OPTION is set to Always.

如果未使用本地缓存,并且遇到此问题,请提交支持请求。If you are not using Local Cache and are experiencing this issue, submit a support request.

我看到消息“以一种访问权限不允许的方式做了一个访问套接字的尝试。”I see the message "An attempt was made to access a socket in a way forbidden by its access permissions." 如何解决此问题?How do I resolve this?

将发生此错误通常是因为 VM 实例上的出站 TCP 连接耗尽。This error typically occurs if the outbound TCP connections on the VM instance are exhausted. 在应用服务中,将强制限制每个 VM 实例的最大出站连接数。In App Service, limits are enforced for the maximum number of outbound connections that can be made for each VM instance. 有关详细信息,请参阅跨 VM 数字限制For more information, see Cross-VM numerical limits.

如果尝试从应用程序访问本地地址,也可能发生此错误。This error also might occur if you try to access a local address from your application. 有关详细信息,请参阅本地地址请求For more information, see Local address requests.

有关 Web 应用中的出站连接的详细信息,请参阅有关到 Azure 网站的传出连接的博客文章。For more information about outbound connections in your web app, see the blog post about outgoing connections to Azure websites.

如何使用 Visual Studio 远程调试应用服务 Web 应用?How do I use Visual Studio to remote debug my App Service web app?

有关如何使用 Visual Studio 调试 Web 应用的详细演练,请参阅 Remote debug your App Service web app(远程调试应用服务 Web 应用)。For a detailed walkthrough that shows you how to debug your web app by using Visual Studio, see Remote debug your App Service web app.