简体   繁体   English

如何减少服务器“等待”时间?

[英]How to reduce server “Wait” time?

I am trying to optimize my site's speed and I'm using the great tool at pingdom.com . 我正在尝试优化我的网站的速度,我正在使用pingdom.com上的伟大工具。 Right now, over 50% of the time it takes to load the page is "Wait" time as shown in the screenshot below. 现在,加载页面所需时间的50%以上是“等待”时间,如下面的屏幕截图所示。 What can I do to reduce this? 我该怎么做才能减少这个? Also, how typical is this figure? 此外,这个数字有多典型? are there benchmarks on this? 这方面有基准吗? Thanks! 谢谢!

高服务器等待时间

EDIT: Ok.. let me clarify a few things. 编辑:好的..让我澄清一些事情。 There are no server side scripts or database calls going on. 没有服务器端脚本或数据库调用正在进行。 Just HTML, CSS, JS, and images. 只是HTML,CSS,JS和图像。 I have already done some things like push js to the end of the body tag to get parallel downloads. 我已经做了一些事情,比如将js推到body标签的末尾以获得并行下载。 I am aware that the main.html and templates.html are adding to the overall wait time by being done synchronously after js.js downloads, that's not the problem. 我知道main.html和templates.html通过在js.js下载后同步完成来增加整体等待时间,这不是问题。 I am just surprised at how much "wait" time there is for each request. 我很惊讶每个请求有多少“等待”时间。 Does server distance affect this? 服务器距离会影响这个吗? what about being on a shared server, does that affect the wait time? 如果在共享服务器上,这会影响等待时间吗? Is there any low-hanging fruit to remedy those issues? 是否有任何悬而未决的成果可以解决这些问题?

在此输入图像描述

The most common reason for this in the case of Apache is the usage of DNS Reversal Lookup. 在Apache的情况下,最常见的原因是使用DNS Reversal Lookup。 What this means is that the server tries to figure out what the name of your machine is, each time you make a request. 这意味着服务器每次发出请求时都会尝试确定您的计算机名称。 This can take several seconds, and that explains why you have a long WAIT time and then a very quick load, because the matter is not about bandwidth. 这可能需要几秒钟,这就解释了为什么你有一个很长的等待时间然后非常快速的负载,因为问题不在于带宽。

The obvious solution for this is to disable hostnamelookup in /etc/httpd/conf/httpd.conf 显而易见的解决方案是在/etc/httpd/conf/httpd.conf中禁用hostnamelookup

HostnameLookups Off

However...this is usually NOT enough. 但是......这通常是不够的。 The fact is that in many cases, apache still does a reversal lookup even when you have disabled host name lookup, so you need to take a careful look at each line of your apache config. 事实是,在许多情况下,即使您已禁用主机名查找,apache仍会执行反转查找,因此您需要仔细查看apache配置的每一行。 In particular, one of the most common reasons for this are LOGS. 特别是,最常见的原因之一是LOGS。 By default on many red hat - centos installations, the log format includes %h which stands for "hostname", and requires apache to do a reverse lookup. 默认情况下,在许多red hat-c​​entos安装中,日志格式包括%h代表“hostname”,并且需要apache进行反向查找。 You can see this here: 你可以在这里看到:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

You should change those %h for %a to solve this problem. 您应该更改%h的%h来解决此问题。

If you have multiple server requests which the page is waiting on, you can make sure that those server requests are sent asynchronously in parallel so that you are serializing them. 如果您有多个页面正在等待的服务器请求,则可以确保这些服务器请求是异步并行发送的,以便您对它们进行序列化。

The slowest possible way to fetch multiple requests is to send one request, wait for its response, send the next request, wait for its response, etc... It's usually much faster to send all requests asynchronously and then process all responses as they arrive. 获取多个请求的最慢可能方式是发送一个请求,等待其响应,发送下一个请求,等待其响应等等...通常以异步方式发送所有请求然后在到达时处理所有响应要快得多。 This shortens the total wait time to the longest wait time for any single request rather than the cumulative wait time of all requests. 这会将总等待时间缩短为任何单个请求的最长等待时间,而不是所有请求的累计等待时间。

If you are only making one single request, then all you can do on the client-side of things is to make sure that the request is sent to the server as early as possible in the page loading sequence so that other parts of the page can be doing their business while the request is processing, thus getting the initial request started sooner (and thus finishing sooner). 如果您只发出一个请求,那么您在客户端可以做的就是确保在页面加载序列中尽早将请求发送到服务器,以便页面的其他部分可以在处理请求时正在开展业务,从而更快地启动初始请求(从而尽快完成)。

The wait time, also known as time to first byte is how long it takes for the server to send the first byte from when the connection is initiated. 等待时间(也称为第一个字节的时间)是服务器从启动连接时发送第一个字节所需的时间。 If this is high, it means your server has got to do a lot of work to render the page before sending it. 如果这个很高,则意味着您的服务器在发送页面之前必须做很多工作才能呈现页面。 We need more information about what your site is doing to render the page. 我们需要有关您的网站正在做什么来呈现页面的更多信息。

TTFB is directly influenced by "physical" distance between browser and server. TTFB直接受浏览器和服务器之间“物理”距离的影响。 CDN proxy is the best way to shorten said distance. CDN代理是缩短距离的最佳方式。 This, coupled with native caching capabilities, will help provide swifter response by loading cached object from the nearest POP (point of placement) location. 这与本机缓存功能相结合,将通过从最近的POP(放置点)位置加载缓存对象来帮助提供更快速的响应。

The effect will depend on user geo-location and CDN's spread. 效果将取决于用户地理位置和CDN的传播。 Still, you can expect significant improvement , 50%-70% or more. 不过,您可以期待50%-70%或更高的显着改善

Speaking from experience, I saw cases in which 90% of content was cached and deliver directly from proxy placed on a different continent, from the other side of the globe. 从经验来看,我看到了一些案例,其中90%的内容被缓存并直接从位于不同大陆的代理发送,来自全球另一端。

This is an issue with the server... According to Pingdom, "The web browser is waiting for data from the server" is what defines the "Wait" time. 这是服务器的问题......根据Pingdom的说法,“Web浏览器正在等待来自服务器的数据”是定义“等待”时间的原因。

There isn't much you can do from a javascript or code end to fix this. 你可以通过javascript或代码端来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM