最简单的2个WordPress速度优化技巧,提高性能让WordPress网站打开速度更快

WordPress教程2年前 (2022)更新 iowen
474 0

让Wordpress网站快速收录以及关键词排名,首先wordpress如果安装过多的插件会导致网站打开速度变慢,从而导致搜索引擎对网站的抓取变得不优好,今天就给大家分享2个WordPress速度优化技巧,提高性能让WordPress网站打开速度更快,以后再也不用安装过多的插件导致网站打开速度变慢。

PHP Opcache

Opcache的全称是: Opcode cache,其前生是Optimizer ,它是PHP的官方公司 Zend 开发的一款闭源但可以免费使用的 PHP 优化加速组件。 Opcode cache 的目地是避免重复编译,减少 CPU 和内存开销。

在PHP 7中, Opcache 是默认安装的,无需另外安装,但需要手动开启。在PHP.ini文件中找到[opcache],参照以下内容进行开启。

[opcache]
;开启opcache
opcache.enable=1  

;CLI环境下,PHP启用OPcache
opcache.enable_cli=1

;OPcache共享内存存储大小,单位MB
opcache.memory_consumption=128 

开启成功后,在PHPinfo中能看到以下内容:

最简单的2个WordPress速度优化技巧,提高性能让WordPress网站打开速度更快
PHPinfo中Opcache信息

WP Super Cache缓存插件

WP Super Cache 是WordPress 最佳的缓存优化插件,另外的W3 Total Cache与它类似, W3 Total Cache虽然功能更强大,但一般的网站使用 WP Super Cache 足够了。相对于 W3 Total Cache ,WP Super Cache的配置更简单。

WP Super Cache的插件主页:https://wordpress.org/plugins/wp-super-cache/

在Wordpress后台插件 — 安装插件,输入WP Super Cache关键词进行查找,即可安装插件。

安装并启用插件后,在后台的设置菜单中会出现WP Super Cache选项,进入配置页面,参照下图进行设置。

最简单的2个WordPress速度优化技巧,提高性能让WordPress网站打开速度更快
WP Super Cache 设置 开启专家模式

开启专家模式后,如果是 Apache 环境, WP Super Cache 会自动修改 .htaccess文件,或者根据提示修改 .htaccess文件内容。如果是 Nginx 环境,需要用以下内容替换Wordpress的伪静态规则。

rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ .*.(oft|eot|svg|ttf|woff|woff2)$
{
	add_header Access-Control-Allow-Origin *; # 解决字体跨站问题
	add_header Access-Control-Allow-Headers X-Requested-With;
	add_header Access-Control-Allow-Methods GET,OPTIONS;
	expires max;
	error_log off;
	access_log /dev/null;
}
# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.
 
set $cache_uri $request_uri;
 
#POST请求和带有查询字符串的网址应始终转到PHP
if ($request_method = POST) {
        set $cache_uri 'null cache';
}
 
if ($query_string != "") {
        set $cache_uri 'null cache';
}   
 
#不要缓存包含以下段的uri
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';
}   
 
#不要将缓存用于登录的用户或最近的评论者
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9] |wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
}
 
# 移动设备支持
# 移动浏览器部分可将其存储为非缓存版本。 默认情况下,由于包括21个在内的大多数现代wordpress主题都具有响应性,因此被评论。 如果要使用WP-Touch之类的插件,请在本节中取消注释配置行
# if ($http_x_wap_profile) {
#        set $cache_uri 'null cache';
#}
 
#if ($http_profile) {
#        set $cache_uri 'null cache';
#}
 
#if ($http_user_agent ~* (2.0 MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo Wii|Nitro|Nokia|Opera Mini|Palm|PlayStation Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915 Obigo|LGE VX|webOS|Nokia5800)) {
 #       set $cache_uri 'null cache';
#}
 
#if ($http_user_agent ~* (w3c |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda |xda-)) {
  #      set $cache_uri 'null cache';
#}
#END 移动设备支持
 
# 使用缓存或实际文件(如果存在),否则将请求传递给WordPress
location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html $uri $uri/ /index.php?$args ;}

如果网站没有开启https访问,最后的部分用以下代码替换:

location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;}

如果开启了移动设备支持(非响应式主题建议开启,PC端和移动端将缓存不同的内容),需要在以上 Nginx 配置中将相应行的#去掉。

WP Super Cache 缓存预加载配置:

最简单的2个WordPress速度优化技巧,提高性能让WordPress网站打开速度更快
© 版权声明

相关文章

暂无评论

暂无评论...