Site environment:
aaPanel
Nginx 1.17
PHP 7.3

  1. Add the following 4 rows to the top of the site configuration.

    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_path /dev/shm/fastcgi-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

  2. Create a cache directory
    mkdir /dev/shm/fastcgi-cache

  3. Comment php configuration

  1. Add the following to the server field
    If you have ssl turned on, add it below #SSL-END

        set $skip_cache 0;
        if ($request_method = POST) {
            set $skip_cache 1;
        }   
        if ($query_string != "") {
            set $skip_cache 1;
        }   
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }   
       if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }
       location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass unix:/tmp/php-cgi-73.sock;
                fastcgi_index index.php;
                include fastcgi.conf;  
                add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache WORDPRESS;
                add_header Cache-Control  max-age=0;
                add_header Nginx-Cache "$upstream_cache_status";
                add_header Last-Modified $date_gmt;
                add_header X-Frame-Options SAMEORIGIN;
                add_header X-Content-Type-Options nosniff;
                add_header X-XSS-Protection "1; mode=block";
                etag  on;
                fastcgi_cache_valid 200 301 302 1d;
        }


        location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow "change_to_your_server_ip";
            deny all;
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
        }
  1. Install and configure the (Nginx Helper) wp plugin.

  2. Since the cache path defined by the plugin author is /var/run/nginx-cache , we may customize the cache path according to the actual situation of the server. As a result, the difference of the cache path will cause the plugin to not find the cache file and delete it!

Add the following code to the WordPress wp-config.php file

  1. Check whether the configuration takes effect
    aaPanel_Jose changed the title to How to enable fastcgi cache for WordPress with aapanel .

    This is working, however the PURGE does not clear the cache. I have had to manually remove the files from this path to clear the cache:
    /dev/shm/fastcgi-cache

    Also let me know, why do we need to comment out PHP Conf files. I am using PHP 7.2 & Nginx 1.16, will there be any difference?

    aaPanel_Jose
    In point number 6 we need to use this code and path to wp-config.php, and then PURGE button of nginx helper will work.

    define('RT_WP_NGINX_HELPER_CACHE_PATH', '/dev/shm/fastcgi-cache');

    When I want to duplicate this procedure on multiple websites, what approach should be used? Since it is giving error message while saving config on another website.

    nginx: [emerg] "fastcgi_cache_key" directive is duplicate

      can you make this as addon or default in next upgrade as cache is most require for wordpress sites

        szone
        So if we need to use fastcgi caching on multiple sites, we need to follow this process: (you need to skip step number 1 from above guide)

        add the following code to nginx.conf located at this path /www/server/nginx/conf/

        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_path /dev/shm/fastcgi-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
        fastcgi_cache_use_stale error timeout invalid_header http_500;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

        And add the following code to each site's config file:

        
         set $skip_cache 0;
                if ($request_method = POST) {
                    set $skip_cache 1;
                }   
                if ($query_string != "") {
                    set $skip_cache 1;
                }   
                if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
                    set $skip_cache 1;
                }   
               if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
                    set $skip_cache 1;
                }
               location ~ [^/]\.php(/|$)
                    {
                        try_files $uri =404;
                        fastcgi_pass unix:/tmp/php-cgi-73.sock;
                        fastcgi_index index.php;
                        include fastcgi.conf;  
                        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
                        fastcgi_cache_bypass $skip_cache;
                        fastcgi_no_cache $skip_cache;
                        add_header X-Cache "$upstream_cache_status From $host";
                        fastcgi_cache WORDPRESS;
                        add_header Cache-Control  max-age=0;
                        add_header Nginx-Cache "$upstream_cache_status";
                        add_header Last-Modified $date_gmt;
                        add_header X-Frame-Options SAMEORIGIN;
                        add_header X-Content-Type-Options nosniff;
                        add_header X-XSS-Protection "1; mode=block";
                        etag  on;
                        fastcgi_cache_valid 200 301 302 1d;
                }
        
        
                location ~ /purge(/.*) {
                    allow 127.0.0.1;
                    allow "change_to_your_server_ip";
                    deny all;
                    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
                }

          JAY
          I think this configuration can be added with an open button or other methods.
          We will join the development plan

            9 days later

            what if i have a non wordpress site how to cache my site..

            4 months later

            can you helpme on this to add cache on non wordpress site.

            2 months later

            how do you cache mobile and desktop versions separately?

            Jose Brother!

            When I do all above configurations along with commenting the include php, worpdress site went to defualt setting of php. I increased the size but nothing happen. Please tell me without ssl site where to put code (what is meant by server field). I put above 4 rows in my nginx.conf, does it safe? In step 6 we create cache location is incorrect? Why my site bypass show instead of hit

            aaPanel_Jose
            When i paste the set $skip_cache 0 ;....... code php went to defualt? When i remove the code, site homepage went change and it start the downloading. The downloaded file contains:
            <?php
            /**

            • Front to the WordPress application. This file doesn't do anything, but loads
            • wp-blog-header.php which does and tells WordPress to load the theme.
              *
            • @package WordPress
              */

            /**

            • Tells WordPress to load the WordPress theme and output it.
              *
            • @var bool
              */
              define( 'WP_USE_THEMES', true );

            /** Loads the WordPress Environment and Template */
            require DIR . '/wp-blog-header.php';

            My environment is:
            Nginx 1.17
            Php 7.4

              aaPanel_Jose
              When I do all above configurations along with commenting the include php, worpdress site went to defualt setting of php. I increased the size but nothing happen. Please tell me without ssl site where to put code (what is meant by server field). I put above 4 rows in my nginx.conf, does it safe? In step 6 we create cache location is incorrect? Why my site bypass show instead of hit

              aaPanel_Jose
              When I do all above configurations along with commenting the include php, worpdress site went to defualt setting of php. I increased the size but nothing happen. Please tell me without ssl site where to put code (what is meant by server field). I put above 4 rows in my nginx.conf, does it safe? In step 6 we create cache location is incorrect? Why my site bypass show instead of hit

                kamranhanif496

                1. The first 4 lines of files added by nginx are fastcgi cache configuration, they are safe
                2. Please do not add these configurations to nginx.conf, add them to the website's configuration file
                3. Even if there is no SSL, there will be # SSL-END comments in the website configuration
                Connect with us: 📨 Telegram 💬 Discord Email: support@aapanel.com