简体   繁体   English

如何减少第一个字节的时间

[英]how to reduce time of first byte

I am using WordPress framework and working on dedicated server from name-cheap and only one site is running on this server.我正在使用 WordPress 框架并使用 name-cheap 的专用服务器工作,并且该服务器上只有一个站点正在运行。 Even after that I'm getting waterfall time in the range of 500ms, but I want to make it around 100ms.即使在那之后,我的瀑布时间也在 500 毫秒范围内,但我想使其在 100 毫秒左右。 This is my website ( http://ucbrowserdownload.net/ ) and the waterfall You can see that everything is perfect from my end but still not getting some solution.这是我的网站 ( http://ucbrowserdownload.net/ ) 和瀑布 从我的角度您可以看到一切都很完美,但仍然没有得到一些解决方案。 Also can check http://labnol.org/ This website is also in WordPress and using same theme, even I am calling very less images or blogs on my index page even I'm error a huge waterfall.也可以查看http://labnol.org/这个网站也在 WordPress 中并使用相同的主题,即使我在索引页面上调用的图像或博客也非常少,即使我犯了一个巨大的瀑布。 Want to know, how to solve all these and to know where is the problem either in WordPress or in theme or in host.想知道如何解决所有这些问题,并想知道 WordPress 或主题或主机中的问题出在哪里。 Completely got stuck and no solution from last few weeks.完全卡住了,过去几周没有解决方案。 Your help will be highly appreciated.您的帮助将不胜感激。 Thank you.谢谢你。 在此处输入图片说明

Original Source 原始来源

Optimization of Nginx Nginx的优化

Optimal Nginx configuration presented in this article.本文介绍的最佳 Nginx 配置。 Once again briefly go through the already known parameters and add some new ones that directly affect TTFB.再次简要介绍已知参数并添加一些直接影响 TTFB 的新参数。

compounds化合物

First we need to define the number of "workers" Nginx.首先我们需要定义“工人”Nginx 的数量。 worker_processes Nginx Each workflow is able to handle many connections and is linked to the physical processor cores. worker_processes Nginx 每个工作流都能够处理许多连接并链接到物理处理器内核。 If you know exactly how many cores in your server, you can specify the number yourself, or trust Nginx:如果您确切知道服务器中有多少个核心,您可以自己指定数量,或者信任 Nginx:

 worker_processes auto; # Determination of the number of working processes

In addition, you must specify the number of connections:此外,您必须指定连接数:

 worker_connections 1024; # Quantification of compounds by one working process, ranging from 1024 to 4096

requests要求

To the Web server can process the maximum number of requests, it is necessary to use a switched off by default directive multi_accept :为了让 Web 服务器可以处理最大数量的请求,有必要使用一个默认关闭的指令 multi_accept :

 multi_accept on; # Workflows will accept all connections

It is noteworthy that the function will be useful only if a large number of requests simultaneously.值得注意的是,该功能只有在同时有大量请求时才有用。 If the request is not so much, it makes sense to optimize work processes, so that they did not work in vain:如果要求不是那么多,那么优化工作流程是有意义的,让他们没有白白工作:

 accept_mutex on; # Workflows will take turns Connection

Improving TTFB and server response time depends on the directives tcp_nodelay and tcp_nopush :改善 TTFB 和服务器响应时间取决于指令 tcp_nodelay 和 tcp_nopush :

 on tcp_nodelay; tcp_nopush on; # Activate directives tcp_nodelay and tcp_nopush

If you do not go into too much detail, the two functions allow you to disable certain features of the TCP, which were relevant in the 90s, when the Internet was just gaining momentum, but do not make sense in the modern world.如果您不详细介绍,这两个功能允许您禁用 TCP 的某些功能,这些功能在 90 年代互联网刚刚获得发展势头时是相关的,但在现代世界中没有意义。 The first directive sends the data as soon as they are available (bypass the Nagle algorithm).第一个指令在数据可用时立即发送数据(绕过 Nagle 算法)。 The second allows you to send a header response (Web page) and the beginning of the file, waiting for filling the package (ie, includes TCP_CORK ).第二个允许您发送响应头(网页)和文件的开头,等待填充包(即,包括 TCP_CORK )。 So the browser can start displaying the web page before.所以浏览器可以先开始显示网页。

At first glance, the functions are contradictory.乍一看,功能是矛盾的。 Therefore, the directive tcp_nopush should be used in conjunction with the sendfile .因此,指令 tcp_nopush 应该与 sendfile 结合使用。 In this case, the packets are filled prior to shipment, as directive is much faster and more optimal than the method of the read + the write .在这种情况下,数据包在发货前被填充,因为指令比 read + the write 的方法更快和更优化。 After the package is full, Nginx automatically disables tcp_nopush , and tcp_nodelay causes the socket to send the data.包满后,Nginx 自动禁用 tcp_nopush , tcp_nodelay 使套接字发送数据。 Enable sendfile is very simple:启用sendfile很简单:

 sendfile on; # Enable more effective, compared to read + write, file sending method

So the combination of all three Directives reduces the load on the network and speeds the sending of files.因此,所有三个指令的组合减少了网络负载并加快了文件的发送速度。

Buffers缓冲器

Another important optimization affects the size of the buffer - if they are too small, Nginx will often refer to the disks are too big - will quickly fill up the RAM.另一个重要的优化影响缓冲区的大小——如果它们太小,Nginx 会经常提到磁盘太大——会很快填满 RAM。 Nginx Buffers To do this, you need to set up four directives. Nginx 缓冲区为此,您需要设置四个指令。 Client_body_buffer_size and client_header_buffer_size set the buffer size for the body and read the client request header, respectively. Client_body_buffer_size 和 client_header_buffer_size 分别设置正文的缓冲区大小并读取客户端请求头。 Of client_max_body_size sets the maximum size of the client request, and large_client_header_buffers specifies the maximum number and size of buffers to read large request headers. client_max_body_size 设置客户端请求的最大大小, large_client_header_buffers 指定读取大请求头的最大缓冲区数和大小。

The optimal buffer settings will look like this:最佳缓冲区设置如下所示:

 10K client_body_buffer_size; client_header_buffer_size 1k; of client_max_body_size 8m; large_client_header_buffers 2 1k; # 10k buffer size on the body of the request, 1 KB per title, 8MB to the query buffer and 2 to read large headlines

Timeouts and keepalive超时和保活

Proper configuration of standby time and keepalive can also significantly improve server responsiveness.适当配置待机时间和保活也可以显着提高服务器响应能力。

Directive client_body_timeout and client_header_timeout set time delay on the body and reading the request header:指令 client_body_timeout 和 client_header_timeout 在 body 上设置时间延迟并读取请求头:

 client_body_timeout 10; client_header_timeout 10; # Set the waiting time in seconds

In the case of lack of response from the client using reset_timedout_connection you can specify Nginx disable such compounds:在客户端使用 reset_timedout_connection 没有响应的情况下,您可以指定 Nginx 禁用此类化合物:

 reset_timedout_connection on; # Disable connections timed-out

Directive keepalive_timeout sets the wait time before the stop connection and keepalive_requests limits the number of keepalive-requests from the same client:指令 keepalive_timeout 设置停止连接前的等待时间,keepalive_requests 限制来自同一客户端的 keepalive-requests 的数量:

 keepalive_timeout 30; keepalive_requests 100; # Set the timeout to 30 and limitations 100 on client requests

Well send_timeout sets the wait time in the transmission response between two write operations:那么send_timeout设置两个写操作之间传输响应中的等待时间:

 send_timeout 2; # Nginx will wait for an answer 2

Caching缓存

Enable caching significantly improve server response time.启用缓存可显着缩短服务器响应时间。 Nginx cache Methods are laid out in more detail in the material about caching with Nginx, but in this case the inclusion of important cache-control . Nginx 缓存方法在关于 Nginx 缓存的材料中有更详细的介绍,但在这种情况下包含重要的缓存控制。 Nginx is able to send a request to redkoizmenyaemyh caching data, which are often used on the client side. Nginx 能够向 redkoizmenyaemyh 缓存数据发送请求,这通常用于客户端。 To do this, the server section you want to add a line:为此,您要在服务器部分添加一行:

 . Location ~ * (jpg | jpeg | png | gif | ico | css | js) $ {expires 365d;}

Targets file formats and duration Cache目标文件格式和持续时间缓存

Also it does not hurt to cache information about commonly used files:缓存有关常用文件的信息也没有坏处:

 open_file_cache max = 10000 = 20s the inactive; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; # Enables the cache tags 10 000 files in 30 seconds

open_file_cache specifies the maximum number of files for which information is stored, and the storage time. open_file_cache 指定存储信息的最大文件数和存储时间。 open_file_cache_valid sets the time after which you need to check the relevance of the information, open_file_cache_min_uses specifies the minimum number of references to the file on the part of customers and open_file_cache_errors includes caching troubleshooting files. open_file_cache_valid 设置您需要检查信息相关性的时间,open_file_cache_min_uses 指定客户对文件的最小引用次数,open_file_cache_errors 包括缓存故障排除文件。

logging日志记录

This is another feature that can significantly reduce the performance of the entire server and, accordingly, the response time and TTFB.这是另一个可以显着降低整个服务器性能的特性,并相应地降低响应时间和 TTFB。 So the best solution is to disable basic log and store information about critical errors only:所以最好的解决方案是禁用基本日志并仅存储有关严重错误的信息:

 off the access_log; the error_log /var/log/nginx/error.log crit; # Turn off the main logging

Gzip compression Gzip压缩

Usefulness Gzip is difficult to overstate.用处 Gzip 怎么强调都不为过。 Compression can significantly reduce traffic and relieve the channel.压缩可以显着减少流量并缓解通道。 But he has a downside - need to compress time.但他有一个缺点——需要压缩时间。 So it will have to turn off to improve TTFB and server response time.因此,它必须关闭以改善 TTFB 和服务器响应时间。 Gzip At this stage, we can not recommend Gzip off as compression improves the Time To Last Byte, ie, the time required for a full page load. Gzip 在此阶段,我们不建议关闭 Gzip,因为压缩会改善 Time To Last Byte,即加载整个页面所需的时间。 And this is in most cases a more important parameter.在大多数情况下,这是一个更重要的参数。 On TTFB and improving server response time greatly affect large-scale implementation of HTTP / 2 , which contains a built-in methods for header compression and multiplexing.在 TTFB 上,提高服务器响应时间极大地影响了 HTTP/2 的大规模实现,其中包含用于标头压缩和多路复用的内置方法。 So that in the future may disable Gzip will not be as prominent as it is now.这样将来可能禁用 Gzip 就不会像现在这样突出。

PHP Optimization: FastCGI in Nginx PHP 优化:Nginx 中的 FastCGI

All sites use modern server technology.所有站点都使用现代服务器技术。 PHP, for example, which is also important to optimize .例如 PHP,这对于优化 . Typically, PHP opens a file, verifies and compiles the code, then executes.通常,PHP 打开一个文件,验证并编译代码,然后执行。 Such files and processes can be set, so PHP can cache the result for redkoizmenyaemyh files using OPcache module.可以设置此类文件和进程,因此 PHP 可以使用 OPcache 模块缓存 redkoizmenyaemyh 文件的结果。 And Nginx, connected to PHP using FastCGI module can store the result of the PHP script to send the user an instant.而Nginx,使用FastCGI 模块连接PHP 可以将PHP 脚本的结果存储在瞬间发送给用户。

The most important最重要的

Optimization of resources and the correct settings for the web server - the main influencing TTFB and server response time factors.资源优化和 Web 服务器的正确设置 - 主要影响 TTFB 和服务器响应时间的因素。 Also do not forget about regular software updates to the stable release, which are to optimize and improve performance.另外不要忘记稳定版本的定期软件更新,这是为了优化和提高性能。

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

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