WordPress 多站点安装 Redis Object Cache 导致网站打不开的解决办法-程序旅途

以前我都是一台服务器部署一个 WordPress 站点,昨天新买了一台配置高的服务器,准备在上面部署多个站点。

第一个站点 A 部署好,安装插件 Redis Object Cache,一切正常。第二个站点 B 部署好,安装插件 Redis Object Cache,启用后,站点 A 就打不开了,登录后台,会 302 到 站点 B。由于 Redis Object Cache 默认在后台不能配置,以前的时候我还抱怨也不能设置连接密码,不过仅仅使用,也就没有深入研究,遇到上面的问题并不知道是 Redis Object Cache 这个插件导致的,昨天搞了一下午也没找到问题所在,无奈又重装了站点 A。

昨天晚上睡得早,半夜12点半又起床,部署第三个站点 C,此时登录着站点 A 的后台 。当站点 C 的 Redis Object Cache 安装启用后,站点 A 的后台瞬间退出,我一下子明白了怎么回事。

两个站点如果同时开启 Redis Object Cache,由于缓存的键是一样的,就导致了后面的覆盖前面的。通过查看文档,发现可以设置以下变量。

Configuration constantDefaultDescription
WP_REDIS_HOST127.0.0.1The hostname of the Redis server
WP_REDIS_PORT6379The port of the Redis server
WP_REDIS_PATHThe path to the unix socket of the Redis server
WP_REDIS_SCHEMEtcpThe scheme used to connect: tcp or unix
WP_REDIS_DATABASE0The database used by the cache: 0-15
WP_REDIS_PREFIXThe prefix used for all cache keys to avoid data collisions, replaces WP_CACHE_KEY_SALT. Should be human readable, not a "salt".
WP_REDIS_PASSWORDThe password of the Redis server. Supports Redis ACLs arrays: ['user', 'password']
WP_REDIS_MAXTTL0The maximum time-to-live of cache keys
WP_REDIS_CLIENTThe client used to communicate with Redis: predisphpredis or relay
WP_REDIS_TIMEOUT1The connection timeout in seconds
WP_REDIS_READ_TIMEOUT1The timeout in seconds when reading/writing
WP_REDIS_IGNORED_GROUPS[]Groups that should not be cached between requests in Redis

其中 WP_REDIS_PREFIX,可以设置缓存键的前缀

分别打开每个站点的 wp-config.php ,在其中设置不同的值

define( 'WP_REDIS_PREFIX', 'xxx' );

将 xxx 替换成你的。