且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-13 09:08:19

在 Apache 的情况下,最常见的原因是 DNS 反向查找的使用.这意味着每次您发出请求时,服务器都会尝试找出您机器的名称.这可能需要几秒钟的时间,这就解释了为什么您的等待时间很长,然后加载速度很快,因为问题与带宽无关.

The most common reason for this in the case of Apache is the usage of 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.

显而易见的解决方案是在/etc/httpd/conf/httpd.conf 中禁用主机名查找

The obvious solution for this is to disable hostnamelookup in /etc/httpd/conf/httpd.conf

HostnameLookups Off

然而……这通常是不够的.事实是,在许多情况下,即使您禁用了主机名查找,apache 仍然会执行反向查找,因此您需要仔细查看 apache 配置的每一行.特别是,最常见的原因之一是日志.默认情况下,在许多 red hat - centos 安装中,日志格式包括 %h 代表主机名",并且需要 apache 进行反向查找.你可以在这里看到:

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. In particular, one of the most common reasons for this are 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. 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

您应该将那些 %h 更改为 %a 以解决此问题.

You should change those %h for %a to solve this problem.