Add directive to make srcache to cache only HTTP 200 OK responses
We have been seeing cases of to many REDIRECT errors on sites with REDIS full page cache.
This happens when a 301 redirect is cached.
https://gridpane.freshdesk.com/a/tickets/418937
On my test site, I see a 301 redirect in cache
root@may29-my-gabriel-ngx:/var/www/first.cloudhapa.xyz/htdocs# redis-cli -s /var/run/redis-page/redis-page.sock -a $(cat /opt/gridpane/object.auth) -n 5
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis /var/run/redis-page/redis-page.sock[5]> GET db5:httpsGETfirst.cloudhapa.xyz/
"HTTP/1.1 301 Moved Permanently\r\nContent-Type: text/html; charset=UTF-8\r\nX-Pingback: https://first.cloudhapa.xyz/xmlrpc.php\r\nX-Redirect-By: WordPress\r\nLocation: https://first.cloudhapa.xyz/\r\n\r\n"
redis /var/run/redis-page/redis-page.sock[5]>
Wordpress when processing URL calls redirect_canonical function which checks if the URL is correctly formed.
If Host header is in UPPERCASE , wordpress will do the following 301 redirect
https://SITE.URL/
to
https://site.url
Steps to recreate the issue
- Create a site and set a static Homepage
- Set full page cache to REDIS
- Make sure that cache is cleared
- Run the following curl simulating a request with host in UPPERCASE
curl -v -L --max-redirs 3 --connect-timeout 10 -H "Host: SITE.URL" "http://site.url"
- Open browser and access the site homepage. The too many redirects error appears
This issue does not occur on sites with FASTCGI cache since it does not cache 301 sites
Solution
Looking the documentation of srcache I found a directive that controls what can be cached.
https://github.com/openresty/srcache-nginx-module?tab=readme-ov-file#srcache_store_statuses
This directive controls what responses to store to the cache according to their status code. By default, only 200, 301, 302, 307 and 308 responses will be stored to cache and any other responses will skip srcache_store. You can specify arbitrary positive numbers for the response status code that you'd like to cache, even including error code like 404 and 503. For example:
srcache_store_statuses 200 201 301 302 307 308 404 503;
At least one argument should be given to this directive.
We need to add the srcache directive to /etc/nginx/common/site.url-wp-redis.conf
srcache_store_statuses 200;
For existing sites we can add the directive using a worker