WordPress优化秘籍

优化方案 服务器使用openresty,php,mysql,memcached,redis 插件:wpjam-…

优化方案

服务器使用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 www.locjj.com;
  • rewrite ^(.*)$ https://www.$host$1 permanent;
  • }
  • server
  • {
  • listen 443 ssl http2;
  • #listen [::]:443 ssl http2;
  • server_name locjj.com 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

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

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

配置Memcached缓存

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

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

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

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

类别:WordPress技巧

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册