• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Nginx directives for WP super cache on Plesk

blueberry

Basic Pleskian
I have tried to add the following nginx directives to

Additional nginx directives​

I only use nginx and I want to use WP super cache in expert mode with Nginx. The following code create the $cache_uri variable, then it checks some exception. Then at the end, it checks if there is an existing cached page in a folder, if yes it returns this page.


Code:
set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $cache_uri 'null cache';
}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $cache_uri 'null cache';
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
    set $cache_uri 'null cache';
}

# Use cached or actual file if they exists, otherwise pass request to WordPress
location ~ / {
    index index.php;
    try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}

However, i think it does not work because it takes 300ms for my server to answer a request when the the file is cached. Usually, static files are delivered in 30ms.
I checked my variables they look ok but I guess the problem could come from the folder path. I don't know in which context I am. is /wp-content the root in this case? Maybe the absolute path should be added?

What do you think?
 
I looked into the cache folder of wp super cache and found that the file was not index.html.
There are two files. Wp super cache chooses the file depending on whether you ticked Compress pages so they’re served more quickly to visitors. (Recommended) or not
1602142189443.png
So since i ticked compress pages,

Code:
location ~ / {
    index index.php;
    try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html.gz $uri $uri/ /index.php ;
}

It still does not work but it is a good start.
 
The thing is basically "the problem" I will describe to you...
The rules you are using are the correct ones... ( I have used Super cache over the years ) but Plesk creates the nginx rules every time you modify parameters of that part on the panel.

This part does not work on Plesk as it should be
location ~ / { index index.php; try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html.gz $uri $uri/ /index.php ; }

Because Plesk inserts a similar rule by code...

You need to modify the Nginx Templates as this documentation shows...

You have to copy
/usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php
to
/usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php

and then modify it in order to include this location ~ / rule as you need it ( it´s already there but is different.. )
Because every time you make a change on the panel you will get this: ( this is an Example of a nginx domain configuration generated file found in /var/www/vhosts/system/domain.zzz/conf/nginx.conf )

1604896871861.png

And you need it like this:

1604897154780.png

The modification you need to do is only add one line...

Change from this lines on /usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php ( do not touch that file )
1604897448628.png

To this modified file /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php ( having this line added )
1604897532988.png



I also create a set of rules in one file regarding all the super cache setup and include it in every domain that i need it
1604897635278.png
my /etc/nginx/include/wordpress has this

Code:
set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
        set $cache_uri 'null cache';
}
if ($query_string != "") {
        set $cache_uri 'null cache';
}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php
|wp-.*.php|/feed/|index.php|wp-comments-popup.php
|wp-links-opml.php|wp-locations.php |sitemap(_index)?.xml
|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {

        set $cache_uri 'null cache';
}

# Don't use the cache for logged-in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+
|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
}

# Set the cache file
set $cachefile "/wp-content/cache/supercache/$http_host/$cache_uri/index.html";
if ($https ~* "on") {
        set $cachefile "/wp-content/cache/supercache/$http_host/$cache_uri/index-https.html";
}


#location ~* /wp-includes/.*.php$ {
#    deny all;
#    access_log off;
#    log_not_found off;
#}

#location ~* /wp-content/.*.php$ {
#    deny all;
#    access_log off;
#    log_not_found off;
#}

location ~* /(?:uploads|files)/.*.php$ {
    deny all;
    access_log off;
    log_not_found off;
}



And there it is... super cache working at nginx level ( awesomely fast ) and not even using cpu resources for PHP ( some extra configuration needed on the plugin )
 
Last edited:
@Cike76 You did an impressive job. Can you tell me what is your time to first byte when a page is cached? (Inspect > Network > reload page). It would be kind to mention how far you are located from your server. (in miles or kilometers) so that i can compare to my situation.
 
Hi,

great topic.

Did you compare this solution with the default Plesk Nginx FastCGI cache?
 
Last edited:
@blueberry , @Cike76

Indeed... you 2 are GOOD!!!! I know all the concepts (why/how) this thread is talking about, but executing/implementing... uhm, servers/networking/nginx is not the top of my skills list.

is it possible to give the summary/settings on the best (ie. fastest performance) you are using?

thanks!!!
Happy New Years

FYI. Plesk Admins, etc ... note: this type of thread SHOULD be important for anyone using Plesk to host their websites.
 
Indeed... you 2 are GOOD!!!! I know all the concepts (why/how) this thread is talking about, but executing/implementing... uhm, servers/networking/nginx is not the top of my skills list.

is it possible to give the summary/settings on the best (ie. fastest performance) you are using?
 
@Cike76
In my installation I couldn't find the location in this file. I guess the directives changed and this lines needs to be added elsewhere?
/usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php
 
Back
Top