to reduce wordpress server response time

Posted by millikan at 2020-02-22

How to configure digital fastcgi cache to shorten WordPress server response time?

In general, nginx configures PHP requests to move to php-fpm because it cannot process PHP code. How to run the LEMP web site. LEMP is decoded into Linux (operating system, program) web server, MySQL (database management system) and PHP (dynamic content processing script server language).

If necessary, php-fpm will ask the MySQL / MariaDB database to get what it wants. Php-fpm generates static HTML pages and then returns the program. Finally, nginx sends a static HTML page to a web browser.

When it comes to static HTML page services, this is very fast. However, it is well known that although the latest version of php7 is much faster than the previous version. A MySQL / MariaDB database is another bottleneck of STECA LEMP website productivity.

Instead of sending a dynamic page request to php-fpm and generating an HTML page, the program can cache the generated HTML page, The next time a cached page is sent to a web browser, in addition to PHP and database requirements.

This will greatly improve the response time of the server and reduce the load on the php-fpm and database servers. This also allows the higher PHP FPM or database server to shut down when nginx maintains web pages.)( When your Linux server is low on memory.

Fastcgi is an inter program program, which is why it is called fastcgi cache.

How to configure digital fastcgi cache

Step 1: edit the main profile

The first parameter specifies the cache location on the file system). Level is set in the / etc / nginx / cache / directory hierarchy. Having a large number of files in a folder can slow down file access, so it is recommended to double-layer subdirectories for most scans. If the level option is not enabled, the program places all files in one directory. The first directory uses a one character name. The subdirectory uses a two character name.

The third parameter specifies the name of the shared memory area (phpcache) and its size( The purpose of this store is to store cache and metadata key, such as usage time. There is a key copy in the memory, which can quickly determine whether a hit or miss request is true or not, and does not need to be transferred to the disk, thus greatly speeding up the verification. The 1 MB area can store data for about 8000 keys, so the 100 MB area can store data for about 800000 keys.

Max u \ size sets the maximum cache size)( If not specified, the cache can use all remaining disk space. When the cache reaches its maximum size, the least used files are removed from the cache.

Data that cannot be obtained in an inactive time (60 minutes) will be removed from the cache by the cache manager, regardless of whether it is out of date or not. The default is 10 minutes. You can also use values such as 12 hours) 12 hours and 7 days.

First, the program records the files used for caching to the temporary store): / var / lib / nginx / fastcgi /. Use U temp \ u path = off to specify the number to write directly to the final directory of the cache to avoid unnecessary data copying between file systems.

Step 2: edit the nginx server block

Open your server block profile.

Scroll down the page to ~ PHP. Add the following lines in this section:

The fastci \ u cache instruction contains the cache, using the store previously created by the fastci \ u cache \ u path instruction.

Nginx can provide stale content from its cache when it cannot get updates from the PHP FPM server. For example, the MySQL / MariaDB database server does not work. Instead of sending errors to customers, the program can provide an obsolete version of the file from its cache. To enable this feature, we added a fastci \ u cache \ use \ u stale directory.

Fastcgi @ u cache u menu uses sets how many times the client requests the element before caching it. The default is 1.

If fastci \ u cache \ u lock is enabled, if more than one client requires a file instead of the current cache, only the first request is allowed on a higher php-fpm server. The rest of the request waits until the request is accepted and the file is extracted from the cache. Without fastcgi. U cache. U lock, all queries are sent directly to a higher PHP FPM server.

Now save and close the server block configuration file. Then check your configuration.

If the test is successful, restart the program.


Cache manager now, and cache folders(

Nginx fastcgi cache test

Take a look at the title x-fastcgi-cache. Hit indicates that the answer is to send from the cache.

Disable caching for some pages

Login, user's cookie file, post query, query line, WordPress server part, website map, channel do not need to be cached. There are no cached registered users or recent commentators. To disable caching for the above items, edit your server block profile. Please insert the location of the following code in the context of the server ~ PHP $.

If you want to add a new entry, make the program skip the fastcgi cache of your IP address.

A string (~) tells nginx that the following is a regular expression. A register whose asterisk * makes regular expressions insensitive. Vertical line for multiple values. If the $remote u addr of a variable matches the regular expression of any IP address, then set $skip \ u cache equal to 1.

Please note that if you use Google XML's website map plug-in in WordPress website, you may see the following rewriting rules in your configuration for your nginx.

These override rules should be placed in the cache policy below. If the coverage rule is higher than the caching policy, your site map will always be cached. Similarly, if you use the yoast SEO plug-in to create a site map, you also need to move the rewrite rule yoast lower than the cache.

Now, insert the following instructions at location ~ ~ PHP $:

The first instruction instructs the program to send a request to a higher php-fpm server instead of trying to find the cache file. The second instruction tells nginx not to cache the answer. Save the file and restart the program.


Materials used from here.