Nginx FastCGI caching and redirects


#1

Hi all,

I ran into a strange behaviour the other day on a site (part of a network) that uses the multi_language plugin Polylang on a server with Nginx caching. The plugin offers an option to detect the visitors browser language upon first visit to the homepage then and automatically redirect them to their preferred language (if available).

The issue was that this redirection seemed to fail, or rather mis-redirect some of the time. And after some testing I could only conclude it was because these redirect responses where cached responses. (Duh)

There is probably no way around this, apart from deactivating the option in the Polylang plugin settings. But maybe someone has some ideas on how to make it work together?

The problem as I see it is that even if these redirect responses could be prevented from being cached (can they?) then the FastCGI Cache would still cache and serve the full home page response (in the default language) also to those that do need a redirect. Effectively this boils down to the same thing as disabling the redirects…

So maybe that would be the best solution to prevent weirder issues when the option is enabled:

Can short redirect responses be prevented from being cached?

That would also be a good solution for a plugin like “Redirection” (nice tool for SEO) since that plugin tries to gather some request info / statistics for certain requests that need a redirection…

And what about 404’s. Are they cached?


#2

@rolf-allard-van-hagen

Let me answer one by one!

You can control which HTTP status to be cached using fastcgi_cache_valid - http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_valid

If you are using a cookie to store value then add cookie into cache key.

For example, if cookie name is “LANG”, then you can use something like:

fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_lang";  

I am not sure why do you need “Redirection plugin” stats. Basically if you are not caching 301/302 status code, then plugin stats should work at the cost of speed.


#3

Thanks Rahul! :slight_smile:

So if I’m reading the Nginx docs correctly changing fastcgi_cache_valid value in

  
location ~ .php$ {  
...  
	fastcgi_cache WORDPRESS;  
	fastcgi_cache_valid  60m;  
}  

to fastcgi_cache_valid 200 60m; should limit caching only to “Status 200 OK” responses?

If you are using a cookie to store value then add cookie into cache key.
The Polylang plugin does indeed use cookies but it always serves different languages via different URLs. Except for the home/root URL, where without caching it can redirect users according to either their browser language (new visitors) or the set cookie (returning visitors) to their dedicated (again, different URLs) language home page... I'm not sure how that cookie value in the fastcgi_cache_key parameter could serve for this case.

#4
I am not sure why do you need “Redirection plugin” stats. Basically if you are not caching 301/302 status code, then plugin stats should work at the cost of speed.
Yes, very aware of the speed cost but warranted by the valuable SEO information it provides. But I agree that this gathering of info should not be left running all the time ;)

Anyway, the fastcgi_cache_valid 200 60m; setting seems to work well on my server. Thanks again! :slight_smile:


#5
.. fastcgi_cache_valid 200 60m; should limit caching only to “Status 200 OK” responses?

Yes. It will not cache redirections.

Regarding cookies, I think you won’t need it as it looks like different URLs are used for different languages.

Try fastcgi_cache_valid 200 60m;. I think it will work nicely. :slight_smile:


#6

For “Redirection plugin” stats…

We use http://rtcamp.com/tutorials/nginx/log-parsing/

Basically I setup a cronjob to email top 404, 301/302, 5xx URLs on daily basis. Based on URL’s and frequency, we make changes to site-content/nginx-config sometimes.


#7

Thanks Rahul :slight_smile:

For those that want to use Nginx fastcgi_cache with Polylang (even on a Multisite) I posted a little rundown and explanation about the steps needed to make it work smoothly on http://wordpress.org/support/topic/nginx-fastcgi_cache-and-polylang-homepage-redirect


#8

Thanks for sharing. :slight_smile:


#9

Wow … I’m really thanks to you … This code help me a lot to speed up my site…


#10