Ghazi Bahri GitHub

How to purge Nginx FastCGI Cache for Mobile & Desktop Users with ngx_cache_purge module

Image pour How to purge Nginx FastCGI Cache for Mobile & Desktop Users with ngx_cache_purge module

ngx_cache_purge is a NGINX module forked from FRiCKLE/ngx_cache_purge which adds ability to purge content from FastCGI, proxy, SCGI and uWSGI caches. A purge operation removes the content with the same cache key as the purge request has.

This tutorial will be showing you how to set up NGINX ngx_cache_purge for Desktop & Mobile users.

Prerequisites

1- NGINX compiled with ngx_cache_purge module. (Nginx -V to check istalled Nginx modules)
2- You’ve already setup and configured Nginx with PHP
3- Caching is enabled

nano /etc/nginx/mobiledetect.map

# Add MAP for Default As desktop
# Match regex = mobile
 
map $http_user_agent $mobile {
default desktop;
"~*ipad" mobile;
"~*android.*mobile" mobile;
"~*iphone" mobile;
"~*ipod.*mobile" mobile;
"~*BlackBerry*Mobile Safari" mobile;
"~*BB*Mobile Safari" mobile;
"~*Opera.*Mini/7" mobile;
"~*IEMobile/10.*Touch" mobile;
"~*IEMobile/11.*Touch" mobile;
"~*IEMobile/7.0" mobile;
"~*IEMobile/9.0" mobile;
"~*Firefox.*Mobile" mobile;
"~*webOS" mobile;
}

nano /etc/nginx/nginx.conf

add : in http {} part :

include /etc/nginx/mobiledetect.map;
fastcgi_cache_key “$scheme$request_method$host$request_uri$mobile”;

none /etc/nginx/sites-available/example.vhost

add
set $var_desktop “desktop”;
set $var_mobile “mobile”;

location ~ /purge(/.*) {
fastcgi_cache_purge cachesd "$scheme$request_method$host$1$var_desktop";
access_log off;
}
 
location ~ /mobilepurge(/.*) {
fastcgi_cache_purge cachesd "$scheme$request_method$host$1$var_mobile";
access_log off;
}

← Retour aux articles