宝塔面板用Nginx openresty+Redis Cache+Memcached缓存提速网站!

以前网上各种教程分享的一直是只能使用redis或memcached其中之一,并没有看到两种缓存加速同时使用的情况。今天我们找到一个教程,就可以二者共存:宝塔面板Memcached 1.6.9+Nginx openresty-1.19.9.1+Redis Cache=wordpress秒级缓存。

用nginx-help插件的时候有两个缓存模式:nginx Fastcgi cache和Redis cache。网上一般都是如何启用nginx Fastcgi cache缓存,但是基本没人说如何开启Redis cache。

宝塔面板用Nginx openresty+Redis Cache+Memcached缓存提速网站!

宝塔面板用Nginx openresty+Redis Cache+Memcached缓存提速网站!

OpenResty自带四个模块

  1. srcache-nginx-module
  2. redis2-nginx-module
  3. HttpRedisModule
  4. set-misc-nginx-module

1.安装Nginx openresty-1.19.9.1和Redis Cach以及Memcached,然后PHP设置里安装下面两个拓展,切记不要安装Reids的PHP拓展。

opcache 缓存器 用于加速PHP脚本! 卸载
memcached 缓存器 比memcache支持更多高级功能 卸载

然后安装水煮鱼的wpjam-basic,复制插件目录里面的object-cache.php到/wp-content目录里面。开启了Memcached。配置好后,我们需要在nginx配置文件(网站配置文件)中部署如下代码:

upstream redis {
            server 127.0.0.1:6379;
            keepalive 512;
    }

之后,继续在网站配置文件:

listen 443 ssl http2;
server_name /www.xxx.com xxx.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/www.xxx.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;
    } 
 
 
     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';
        add_header X-Cache "$srcache_fetch_status From $host";
        add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套
        add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型
        add_header X-XSS-Protection "1; mode=block"; # XSS 保护   
    
        fastcgi_pass unix:/tmp/php-cgi-74.sock;     #这里php版本不一样请注意修改
        fastcgi_index index.php;
        include fastcgi.conf;
    }

配置成功后重启nginx:用命令 service nginx restart 或者自己在宝塔面板的nginx管理中重启nginx。

下一步安装nginx-help插件,选择Redis cache模式即可。

退出出登录刷新页面,看见Hit就是成功了。

配置多站点共存,在wp网站配置文件wp-config.php内加入下面代码即可。

define('WP_CACHE_KEY_SALT', 'yoursite.com');

提示:不要安装Redis Object Cache这个插件,需要使用我爱水煮鱼的WPJAM自带的启用Memcached。

教程!宝塔面板如何开启Nginx fastcgi_cache缓存为WordPress提速?

上一篇

Nginx配置多站点下如何设置Fastcgi_cache缓存加速?

下一篇

99%的人还看了

发表评论

插入图片

欢迎登陆本站

 | 注册

必应搜索 站长推荐

在线工具

VIP视频解析

衡天云优质VPS 站长推荐

注册

登录

忘记密码 ?