Hello,
First of all I'd like to note that using this code is at your own responsibility, therefore I sincerely recommend using some sort of backup site first as this is still in testing fase.
Known issues (feel free to help out)
- Cache can not be cleared for now, I am currently working on it but if you have any ideas on how to empty a cache directory feel free to help out. For now you can do the following:
- 'sudo rm -rf /etc/nginx/cache'
- 'sudo mkdir /etc/nginx/cache'
- 'sudo chmod 777 /etc/nginx/cache'
- Adding content changing buttons through code is annoying, I am looking for a way to simplify this but I havent found one yet.
Feel free to help me out with this project so that we can all enjoy it. But nonetheless, let's get right into it!
Why Nginx Full Page Caching?
Nginx full page caching basically is the same as varnish full page caching. The only two differences here are that NGinx is the reverse proxy webserver and can decide immediately wether a URL needs to be cached or not, instead of (like varnish) passing the url to a different webserver. And, the fact that it can cache large PHP queries, which will speed up the server as well.
What is the average speed you can gain?
In my testing environments I achieved results that were mindblowing, they showed that the store could achieve response times in under 100ms which resulted in page loads under 300ms!
Requirements?
- Server knowledge (Unix in particular, this was also the OS I used for testing)
- Some coding skills (for linking the buttons to javascript code)
- NGinx as your webserver
- CS-Cart 4.5.2 (only version that was tested, it might be compatible with older versions as well).
- MAKE SURE THAT THE URL'S YOU USE ARE LANGUAGE DEPENDENT. EG.
- demostore.com/en/beautiful_categorie/product-en
- demostroe.com/de/schoene_kategorie/produkt-de
- demostore.com/nl/mooie-categorie/product-nl
Make CS-Cart ready!
1. Upload the provided package
2. Add the following class 'nginx-no-cache' to buttons who can change content (think about buttons who will cause a permanent change in the page, e.g. login button.)
3. Install some sort of cookie finder on the page and check if the cookie 'no-cache' is there whenever you press a button, or do anything on the page (this is very important, otherwise clients might receive old cache).
Make NGinx ready!
1. Make a backup of your config file (etc/nginx/sites-available/default)
2. Make the cache directory: (sudo mkdir /etc/nginx/cache) and give it 777 permissions.
3. Add the following code to the top of the file (!!!! not inside the 'server {' !!!!) (You can change the inactive time in case you want to)
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=microcache:1024m inactive=2h; add_header X-Cache $upstream_cache_status;
4. Make sure to add the following nginx config in the 'server {'. Furthermore you should add your own exception URL's (THIS IS VERY IMPORTANT, IF YOU HAVE LANGUAGES SHARING THE SAME URL INCLUDE IT HERE), this is very important as I do not know what seo changes you might have done
# Cache everything by default set $no_cache 1;# Only cache GET requests if ($request_method != GET){ set $no_cache 1; } # Don't cache the following URLs if ($request_uri ~* "/(YOUR ADMIN URL|dispatch=checkout.checkout|dispatch=checkout.cart)"){ set $no_cache 1; } # Don't cache peeps with cookies (addon) if ($http_cookie ~* "no_cache"){ set $no_cache 1; }
5. Now we have to add something to the following segment:
# Processing PHP scripts location ~ \.php$ {
It should look like this in the end
# Processing PHP scripts location ~ \.php$ { root /data/www/****; proxy_read_timeout 61; fastcgi_read_timeout 61; try_files $uri $uri/ =404;# The path to the PHP-FPM daemon socket fastcgi_pass unix:/run/php/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # fastcgi caching fastcgi_cache microcache; fastcgi_cache_key $scheme$host$request_uri$request_method; fastcgi_cache_valid 200 301 302 3h; fastcgi_cache_use_stale updating error timeout invalid_header http_500; fastcgi_pass_header Set-Cookie; fastcgi_pass_header Cookie; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; fastcgi_cache_bypass $no_cache; fastcgi_no_cache $no_cache;
}
6. Now save the config file (and double check it)
7. Restart nginx (sudo service nginx restart)
What will the final version include?
1. Cache will be cleared whenever you want to (just like normal, demostore.com/admin.php?cc&ctpl).
2. Buttons will be manageable through a nice UI in the backend.
3. Toggle the nginx proxy cache through the settings menu
4. And many more things to come!
Some final words
First of all I'd like to thank hungryweb for sharing some of his knowledge with me. Secondly, I'd like to remark that this addon is in beta and that I will release an official version with many more features soon. Feel free to help me out developing it and give me any tips and tricks! I ofcourse will be happy to assist you if you have any questions regarding this topic!
Have a good one,
PoppedWeb (and HungryWeb)