欢迎来到好人卡资源网,专注网络技术资源收集,我们不仅是网络资源的搬运工,也生产原创资源。寻找资源请留言或关注公众号:烈日下的男人

2021WordPress速度优化秘籍

网站源码 sky995 3年前 (2021-02-13) 948次浏览 0个评论

本文及资源最后更新时间 2021-02-13 by sky995

优化方案

本站运行在阿里云学生机1c2g5m上,运行情况可以看这里–>探针

服务器使用openresty,php,mysql,memcached,redis

插件:wpjam-basic,Nginx-helper

详细步骤

配置redis缓存

首先要安装好上述所有程序

然后修改网站的配置文件(vhost conf)

下面直接贴上我的配置文件,大家可以照修改

  • upstream redis {
  • server 127.0.0.1:6379;
  • keepalive 512;
  • }
  • server
  • {
  • listen 80;
  • #listen [::]:80;
  • server_name locjj.com http://www.locjj.com;
  • rewrite ^(.*)$ https://www.$host$1 permanent;
  • }
  • server
  • {
  • listen 443 ssl http2;
  • #listen [::]:443 ssl http2;
  • server_name locjj.com http://www.locjj.com;
  • index index.html index.htm index.php default.html default.htm default.php;
  • root /home/wwwroot/locjj.com;
  • set $skip_cache 0;
  • #POST请求直接调用后端
  • 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 /redis-fetch {
  • internal ;
  • set $redis_key $args;
  • redis_pass redis;
  • }
  • location /redis-store {
  • internal ;
  • set_unescape_uri $key $arg_key ;
  • redis2_query set $key $echo_request_body;
  • redis2_query expire $key 14400;
  • redis2_pass redis;
  • }
  • ssl_certificate /home/wwwroot/ssl/1.crt;
  • ssl_certificate_key /home/wwwroot/ssl/1.key;
  • ssl_session_timeout 5m;
  • ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  • ssl_prefer_server_ciphers on;
  • ssl_ciphers “TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5”;
  • ssl_session_cache builtin:1000 shared:SSL:10m;
  • # openssl dhparam -out /usr/local/openresty/nginx/conf/ssl/dhparam.pem 2048
  • ssl_dhparam /usr/local/openresty/nginx/conf/ssl/dhparam.pem;
  • include rewrite/wordpress.conf;
  • #error_page 404 /404.html;
  • # Deny access to PHP files in specific directory
  • #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
  • # include enable-php.conf;
  • location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  • {
  • expires 30d;
  • }
  • location ~ .*\.(js|css)?$
  • {
  • expires 12h;
  • }
  • location ~ /.well-known {
  • allow all;
  • }
  • location ~ /\.
  • {
  • deny all;
  • }
  • access_log off;
  • location ~ [^/]\.php(/|$) {
  • set $key “nginx-cache:$scheme$request_method$host$request_uri”;
  • try_files $uri =404;
  • srcache_fetch_skip $skip_cache;
  • srcache_store_skip $skip_cache;
  • srcache_response_cache_control off;
  • set_escape_uri $escaped_key $key;
  • srcache_fetch GET /redis-fetch $key;
  • srcache_store PUT /redis-store key=$escaped_key;
  • more_set_headers ‘X-Cache $srcache_fetch_status’;
  • more_set_headers ‘X-Store $srcache_store_status’;
  • #PHP版本号有出入的别弄错了
  • fastcgi_pass unix:/tmp/php-cgi.sock;
  • fastcgi_index index.php;
  • include fastcgi.conf;
  • fastcgi_param PHP_VALUE “open_basedir=$document_root:/tmp/:/proc/”;
  • }

修改好后重启openresty,去wordpress后台安装Nginx-Helper插件,开启redis缓存,其他全部默认即可

这个插件的作用是能够及时的刷新缓存

命中情况可以在服务器上执行

  • telnet 127.0.0.1 6379

 

2021WordPress速度优化秘籍

keyspace_hits为命中次数,keyspace_misses为未命中次数

这里可以看到命中率还是很高的

配置Memcached缓存

在wordpress后台安装wpjam-basic插件,然后点击扩展管理,微信扫码登录

然后将/wp-content/plugins/wpjam-basic/template/object-cache.php复制到/wp-content

即可开启Memcached缓存,点击WPJAM-系统信息可以查看详情

2021WordPress速度优化秘籍

WPJAM插件还有其他很多优化功能,大家可以自己摸索

有人说Redis 和 Memcached 是同一类缓存吧,用其中一个就好了。

我的方案是Openresty->Redis(未命中->php->Memcached(未命中->mysql))
两者是不会冲突的


好人卡资源网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:2021WordPress速度优化秘籍
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址