Vesta Control Panel - Forum

Community Forum

Skip to content

Advanced search
  • Quick links
    • Main site
    • Github repo
    • Google Search
  • FAQ
  • Login
  • Register
  • Board index Main Section Web Server
  • Search

[HOWTO] FASTCGI CACHE WORDPRESS Topic is solved

Questions regarding the Web Server
Apache + Nginx, Nginx + PHP5-FPM
Post Reply
  • Print view
Advanced search
14 posts
  • 1
  • 2
  • Next
valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
[HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Sat Sep 01, 2018 6:46 am

NGINX + PHP-FPM + WORDPRESS + NGINX CACHE SNIPER
I found a good solution to cache wordpress.
Nginx includes a FastCGI module which has directives for caching dynamic content that are served from the PHP backend. Setting this up removes the need for additional page caching solutions like reverse proxies (think Varnish) or application specific plugins (Super Cache). Content can also be excluded from caching based on the request method, URL, cookies, or any other server variable.
Cache Sniper for Nginx works as it should only if script have permissions to cache files and folders. To make it works you have to change the web user (%user%) to www-data which is the default user that nginx uses. Then your PHP script run through php-fpm will have the appropriate permissions and will be able to delete the cache.

Go to /usr/local/vesta/data/templates/web/php-fpm and make copy socket.tpl to www-data-socket.tpl
Open terminal:

Code: Select all

cd /usr/local/vesta/data/templates/web/php-fpm/
cp socket.tpl www-data-socket.tpl
sudo chmod 664 www-data-socket.tpl
nano www-data-socket.tpl
Find:

Code: Select all

user = %user%
      group = %user%

      listen.owner = %user%
      listen.group = www-data

and change with:

Code: Select all

                      user = www-data
                               group = www-data

                               listen.owner = %user%
                               listen.group = www-data
                               listen.mode = 0660
Go to /usr/local/vesta/data/templates/web/nginx/php-fpm and make copy wordpress2_rewrite.tpl, wordpress2_rewrite.stpl to wordpress2_rewrite_nginx_sniper.tpl, wordpress2_rewrite_nginx_sniper.stpl

Code: Select all

cd /usr/local/vesta/data/templates/web/nginx/php-fpm/
cp wordpress2_rewrite.tpl wordpress2_rewrite_nginx_sniper.tpl
cp wordpress2_rewrite.stpl wordpress2_rewrite_nginx_sniper.stpl
sudo chmod 664 wordpress2_rewrite_nginx_sniper.*

Code: Select all

nano wordpress2_rewrite_nginx_sniper.tpl
and after

Code: Select all

nano wordpress2_rewrite_nginx_sniper.stpl
At the beginning add

Code: Select all

fastcgi_cache_path /var/cache/nginx/fastcgi_cache/%domain%/ levels=1:2 keys_zone=%domain%:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;
Next, move the location directive that passes PHP requests to php-fpm. Inside "location ~ .php$ { }" add the following lines.

Code: Select all

fastcgi_cache_valid 200 301 302 60m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache %domain%;
Some dynamic content such as authentication required pages shouldn't be cached.
Such content can be excluded from being cached based on server variables.
Here is a configuration which must be used in the server{ } context.

Code: Select all

#Cache everything by default
set $skip_cache 0;

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

    #Don't cache if the URL contains a query string
    if ($query_string != "") {
        set $skip_cache 1;
    }   

    # Don’t cache uris containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }   

    # Don’t use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }
    
    # Skip cache on WooCommerce pages
    if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
        set $skip_cache 1;
    }
To apply the "$no_cache" variable to the appropriate directives, place the following lines inside location ~ .php$ { }

Code: Select all

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
All together looks like:Show
fastcgi_cache_path /var/cache/nginx/fastcgi_cache/%domain%/ levels=1:2 keys_zone=%domain%:200m inactive=120m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

add_header X-Cache $upstream_cache_status;


server {
listen %ip%:%web_port%;
server_name %domain_idn% %alias_idn%;
root %docroot%;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/%domain%.log combined;
access_log /var/log/nginx/domains/%domain%.bytes bytes;
error_log /var/log/nginx/domains/%domain%.error.log error;
location = /favicon.ico {
log_not_found off;
access_log off;
}

#Cache everything by default
set $skip_cache 0;

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

#Don't cache if the URL contains a query string
if ($query_string != "") {
set $skip_cache 1;
}

# Don’t cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}

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

# Skip cache on WooCommerce pages
if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*") {
set $skip_cache 1;
}


location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
try_files $uri $uri/ /index.php?$args;

if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}

location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}

location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;


fastcgi_pass %backend_lsnr%;
fastcgi_index index.php;

include /etc/nginx/fastcgi_params;


fastcgi_cache_valid 200 301 302 60m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache %domain%;

}
}

error_page 403 /error/404.html;
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;

location /error/ {
alias %home%/%user%/web/%domain%/document_errors/;
}

location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}

location /vstats/ {
alias %home%/%user%/web/%domain%/stats/;
include %home%/%user%/web/%domain%/stats/auth.conf*;
}

include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;

include %home%/%user%/conf/web/nginx.%domain_idn%.conf*;
}
Make your cache directory
Open terminal:

Code: Select all

mkdir -p  /var/cache/nginx/fastcgi_cache
chown -R www-data /var/cache/nginx/fastcgi_cache
chmod 700 /var/cache/nginx/fastcgi_cache
and change owner to youruser web folder

Code: Select all

chown -R www-data:www-data /home/youruser
Open terminal: save, update and restart

Code: Select all

v-update-web-templates
service nginx configtest
service nginx restart
systemctl restart php7.2-fpm
Open Wordpress Plugins (yourdomain.php/wp-admin/plugins.php)
Add and activate Cache Sniper for Nginx

And set your fastcgi_cache_path to plugin
Cache Path settings /var/cache/nginx/fastcgi_cache/yourdomain.com
Cache Levels settings 1:2
Image

You can controll if you cache or not, and how long you cache page by page
Homepage cache already controlled by plugin last update.
So, open index.pxp and put in the beginning to be sure that your main page cache renew every 10 min.

Code: Select all

<?php
if (is_front_page() ) {
   header("X-Accel-Expires: 600"); // cache main page for a while the big 10 minutes
} 
?>
<html>
<body>

...
...
Good luck with everything!
Last edited by valentine on Thu Sep 20, 2018 10:48 am, edited 14 times in total.
Top

xorro
Posts: 87
Joined: Sun Nov 13, 2016 3:11 pm
Contact:
Contact xorro
Website Skype

Os: CentOS 6x
Web: apache + nginx
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by xorro » Fri Sep 07, 2018 1:26 am

Can you explain this line

Code: Select all

Next, move the location directive that passes PHP requests to php5-fpm. Inside "location ~ .php$ { }" add the following lines.
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Fri Sep 07, 2018 5:20 am

xorro wrote: ↑
Fri Sep 07, 2018 1:26 am
Can you explain this line
Next, move the location directive that passes PHP requests to php5-fpm. Inside "location ~ .php$ { }" add the following lines.
Of course, you can check spoiler, where all lines together. But I have php 7.2 onboard.
Last edited by valentine on Mon Sep 10, 2018 11:22 am, edited 1 time in total.
Top

xorro
Posts: 87
Joined: Sun Nov 13, 2016 3:11 pm
Contact:
Contact xorro
Website Skype

Os: CentOS 6x
Web: apache + nginx
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by xorro » Fri Sep 07, 2018 7:31 pm

I am sorry for my short question before what i wanted to ask was in which file do we need to put this code?
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Fri Sep 07, 2018 10:46 pm

vesta php-fpm templates: wordpress2_rewrite_nginx_sniper.tpl and wordpress2_rewrite_nginx_sniper.stpl

Code: Select all

cd /usr/local/vesta/data/templates/web/nginx/php-fpm/
cp wordpress2_rewrite.tpl wordpress2_rewrite_nginx_sniper.tpl
cp wordpress2_rewrite.stpl wordpress2_rewrite_nginx_sniper.stpl
sudo chmod 664 wordpress2_rewrite_nginx_sniper.*
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Sun Sep 09, 2018 9:48 pm

UPDATED. U MAKE MISTAKE IN MANUAL. $skip_cache/$no_cache
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Mon Sep 10, 2018 10:57 am

UPDATE. ADD

Code: Select all

fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
BECAUSE viewtopic.php?f=29&t=15764&p=64967&hili ... che#p64967
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Mon Sep 10, 2018 11:33 am

Also use this for browser cache:

Code: Select all

location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js|eot|otf|woff|woff2|ttf|ogg)$ {
expires max;
fastcgi_hide_header "Set-Cookie";
} 
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Thu Sep 20, 2018 10:44 am

Cache Sniper for Nginx UPDATE
1.0.4.1

Fixed a bug that caused wp_insert_comment to fail.

1.0.4

Automatically clearing the homepage cache on content create/update/delete.
So, last step no need anymore.
open index.pxp and put in the beginning to be sure that your main page cache renew every 10 min.

Code: Select all

<?php
if (is_front_page() ) {
   header("X-Accel-Expires: 600"); // cache main page for a while the big 10 minutes
} 
?>
<html>
<body>

...
...
Top

valentine
Posts: 16
Joined: Sat Sep 01, 2018 4:10 am

Os: Ubuntu 15x
Web: nginx + php-fpm
Re: [HOWTO] FASTCGI CACHE WORDPRESS
  • Quote

Post by valentine » Sat Sep 22, 2018 9:53 am

Image

You want your site to be fast, so you added caching… but now you have a new challenge that you have content that shouldn’t be caching such as ads, feeds, rotating ads, videos, shopping carts and more..

With No Cache AJAX Widgets, you simply drag and drop the new AJAX powered widgets to any widget area/s, add your content and you are done. No more messing with your theme files, functions.php, fragmented caching, mfunc and others or excluding an entire page from caching.
Top


Post Reply
  • Print view

14 posts
  • 1
  • 2
  • Next

Return to “Web Server”



  • Board index
  • All times are UTC
  • Delete all board cookies
  • The team
Powered by phpBB® Forum Software © phpBB Limited
*Original Author: Brad Veryard
*Updated to 3.2 by MannixMD
 

 

Login  •  Register

I forgot my password