Teach you how to use fastcgi_cache to speed up WordPress

WordPress has many cache acceleration solutions, such as plug-in cache (wp-super-cache, wp-rocket, etc.), PHP code cache, etc., and now share the nginx cache used on this site. Take advantage of fastcgi_cache caching.

Before using nginx cache, a special module must be loaded in nginx, this module is called ngx_cache_purge.

Add ngx_cache_purge module

Download the ngx_cache_purge module

The official address of the ngx_cache_purge module: http://labs.frickle.com/files/. Find the latest version of the module at this address and download it using wget.

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz

I am using ngx_cache_purge-2.3 here.

Compile and install the ngx_cache_purge module

Use the nginx -V command to check whether nginx has installed this module. If it is not installed, you need to recompile and install it.

Students who use the one-click installation package of Junge lnmp can find the lnmp.conf file in the lnmp installation directory, and then add ngx_cache_purge to the nginx module. After that, you can smoothly upgrade nginx again.

Modify ngxin configuration

Before using fastcgi_cache cache, you must first modify the nginx configuration, specifically, enter the virtual host configuration, find domainname.conf, and then modify the sever configuration inside.

#Please create the wpcache path in the following 2 lines in advance, otherwise the path may not exist and nginx cannot be started. Please set max_size according to the partition size.
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
#Ignore all nocache declarations, avoid not caching pseudo-static, etc.
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#Ps: If there are multiple sites, do not add the above content repeatedly, otherwise it will conflict. You can consider adding the above content to nginx.conf to avoid adding it multiple times.
server
    {
        listen 80;
        #Please modify it to your own domain name
        server_name zhangge.net;
        index index.html index.htm index.php default.html default.htm default.php;
        #Please modify it to the storage path of your own website
        root  /home/wwwroot/domainname.com;
        set $skip_cache 0;
        #post access is not cached
        if ($request_method = POST) {
            set $skip_cache 1;
        }
        #Dynamic query is not cached
        if ($query_string != "") {
            set $skip_cache 1;
        }
        #Specific pages such as the background are not cached (please add other requirements by yourself)
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }
        #Do not display the cache for logged-in users and users who have commented (this rule is not used by Zhang Ge's blog, everyone sees the cache)
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }
        #Please refer to the previous configuration of your website here, especially the sock path, if it is wrong, it will be 502!
        location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #Added cache rules
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache WORDPRESS;
                fastcgi_cache_valid 200 301 302 1d;
        }
        location / {
                #You can add custom pseudo-static rules here (the pseudo-static rules you added before can be added here, if you don't have them, you don't need them)
                try_files $uri $uri/ /index.php?$args;
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
         }
        #Cache cleaning configuration (optional module, please read the description below)
        location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow "Fill in the real external IP of your server here";
            deny all;
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
        }
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off; log_not_found off; expires max;
        }
        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /\. { deny  all; access_log off; log_not_found off; }
        #Please note to modify the log path
        access_log /home/wwwlogs/domainname.com.log access;

Pay attention to modifying the modified part in the above code, otherwise nginx will restart and an error will occur. Of course, if https is enabled, the module should be changed accordingly.

Install the Nginx-helper plugin

Search for nginx-helper in the background and install the plugin.

About plugin settings:

If you don't use CDN, you can choose purge mode, if you use CDN, it's better to choose file mode.

Since the cache path defined by the plugin author is /var/run/nginx-cache, and we may customize the cache path according to the actual situation of the server, the different cache paths will cause the plugin to fail to find the cache file and delete it!

Solution: Add a line of code to wp-config.php:

define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

This way, it is configured.

 

 

 

This article is reproduced from: https://www.linuxprobe.com/fastcgi-cache.html

Provide the latest Linux technology tutorial books for free, and strive to do more and better for open source technology enthusiasts, open source site: https://www.linuxprobe.com/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325110085&siteId=291194637