Use Nginx Fastcgi_cache cache to accelerate optimization of WordPress

Source: Internet
Author: User
Tags wordpress cache

The core principle of high concurrent Web site architecture In fact, "all the user access requests are as far as possible forward", that is: can slow the existence of user computer Local, do not let him visit the CDN. Can cache CDN Server, do not let CDN to access the source (static server). If you can access a static server, do not access the dynamic server. And so on: Without access to the database and storage must not access the database and storage.

WordPress The best way to optimize is not to install Plug-ins, WordPress is a typical php-mysql application, to do the database cache, rather let the lightweight nginx directly to cache WordPress content.

Nginx built-in fastcgi caching, but does not support automatic cleanup of caches. When you create/modify an article in WordPress, or when a visitor submits a comment, it is necessary to automatically empty the relevant cache! Nginx need to install ngx_cache_purg+ customized wordpress cache cleaning Plug-ins: Nginx Helper.

1. Install Nginx ngx_cache_purge Module


1) to see if the Ngx_cache_purge is installed

Nginx-v 2>&1 | Grep-o Ngx_cache_purge

Show Ngx_cache_purge indicates already installed

2) Install Ngx_cache_purge module

"Oneinstack" and "Lnmp one-click installation Package" under the installation of Ngx_cache_purge module

The code is as follows:


cd/root/oneinstack/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http:// nginx.org/download/nginx-1.9.10.tar.gz
Tar xzf ngx_cache_purge-2.3.tar.gz
Tar xzf nginx-1.9.10.tar.gz
CD nginx-1.9.10
Nginx-v #查看nginx编译参数, and finally add--add-module=. /ngx_cache_purge-2.3
./configure--prefix=/usr/local/nginx--user=www--group=www \
--with-http_stub_status_ Module--with-http_v2_module--with-http_ssl_module \
--with-ipv6--with-http_gzip_static_module--with-http_ Realip_module \
--with-http_flv_module--with-ld-opt=-ljemalloc \
--add-module= ... /ngx_cache_purge-2.3
Make #编译
mv/usr/local/nginx/sbin/nginx{,_ ' date +%f '} #备份nginx
CP objs/nginx/usr/ Local/nginx/sbin
Nginx-v 2>&1 | grep-o ngx_cache_purge
# display ngx_cache_purge indicates successful installation


2. Nginx Configuration
It is recommended that the Fastcgi_cache_path be set TMPFS in memory and the operating system will have different TMPFS paths, as follows:

Centos:/dev/shm
Ubuntu and Debian:/run/shm

Modify the Nginx virtual host configuration file/usr/local/nginx/conf/vhost/blog.linuxeye.com.conf:

The code is as follows:
Fastcgi_cache_path/dev/shm/nginx-cache levels=1:2 keys_zone=wordpress:100m inactive=60m;
Fastcgi_cache_key "$scheme $request_method$host$request_uri";
Fastcgi_cache_use_stale error timeout Invalid_header http_500;
Fastcgi_ignore_headers Cache-control Expires Set-cookie;
server {
Listen 443 SSL HTTP2;
SSL_CERTIFICATE/USR/LOCAL/NGINX/CONF/VHOST/LINUXEYE_BLOG.CRT;
Ssl_certificate_key/usr/local/nginx/conf/vhost/linuxeye_blog.key;
Ssl_ciphers "chacha20:gcm:high:! Dh:! rc4:!anull:!enull:! low:!3des:! md5:! Exp:! Psk:! srp:! DSS ";
Ssl_session_cache shared:ssl:10m;
Ssl_session_timeout 10m;
#ssl_stapling on;
#ssl_stapling_verify on;
Resolver 8.8.8.8 8.8.4.4 valid=300s;
Resolver_timeout 5s;
server_name blog.linuxeye.com;
Access_log/home/wwwlogs/blog_nginx.log combined;
Index index. Html index.htm index.php;
Include wordpress.conf;
Root/home/wwwroot/blog;
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 (/|$) {
#fastcgi_pass remote_php_ip:9000;
Fastcgi_pass Unix:/dev/shm/php-cgi.sock;
Fastcgi_index index.php;
Include fastcgi.conf;
Fastcgi_cache_bypass $skip _cache;
Fastcgi_no_cache $skip _cache;
Fastcgi_cache WORDPRESS;
Fastcgi_cache_valid 60m;
}
Location ~/purge (/.*) {
Allow 127.0.0.1;
Deny all;
Fastcgi_cache_purge WORDPRESS "$scheme $request_method$host$1";
}
Location ~. *\. (Gif|jpg|jpeg|png|bmp|swf|flv|ico) $ {
Expires 30d;
Access_log off;
}
Location ~. *\. (js| CSS)? $ {
Expires 7d;
Access_log off;
}
}


Make Nginx Configuration effective


The code is as follows:
Service Nginx Reload


After restarting the system, the SHM in-memory Nginx-cache folder is lost, in order for the reboot to take effect (automatically create the folder), modify/etc/init.d/nginx's make_dirs to add the following line:


The code is as follows:
[!-d '/dev/shm/nginx-cache '] && {mkdir/dev/shm/nginx-cache; Chown-r ${user}. $user/dev/shm/nginx-cache;}


3. WordPress Installs Nginx Helper Plugin
WordPress Background "Plug-in"-"Install plugin" search "Nginx Helper" installation can be. The following settings:

Modify the WordPress Web site root wp-config.php Add the following line:


The code is as follows:
Define (' Rt_wp_nginx_helper_cache_path ', '/dev/shm/nginx-cache ');

4. Test
Check the Enable Logging (logging), enable Nginx Timestamp in HTML (insert cache information) when testing
To see if the Nginx helper refreshes the log:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.