Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am maintaining a website with 3 different versions of the site, with 3 different languages, handles with a single system written in php, which takes in environment variables based on the domain name that is being accessed. These are the three sites:

  • : english international version
  • : european version of site
  • : russian version of the site

when is accessed from russia it is to be redirected to, and any country from europe accesses, is redirected to, and international visitors stay at, although they can go to the country specific site. All these redirections for the country is done using GeoIP apache2 mod in order to determine the country code, which is used in a RewriteCondition to state a RewriteRule, there are some exceptions of IPs that do not do the rewrite for, basically the IPs of the developer's PCs. The site has been doing just fine, until we decided to setup varnish to give the site a boost, it really did give it a great boost, but the country specific rewrites has become buggy.

What started to happen is that a russian visitor can go to and won't be redirected, until he clicks a random link (perhaps a link not cached by varnish yet) and the user is redirected to their specific country.

For that i setup mod_rpaf, and for exceptions to the rewrite rule (for the developer's ip), i used this RewriteCond %{HTTP:X-FORWARDED-FOR} !^43\.43\.43\.43, and i restarted varnish and apache2, it worked for a while, then it messed up again.

And whole day i have been doing changes however i have little no clue as to what's going on, sometimes it works, and sometimes it doesn't, and sometimes it half works, etc...

As for geoip, i used a php to check the $_SERVER variable, and here is the general idea of the output

[HTTP_X_VARNISH] => 1705675599

Now, thanks to the "random" redirects, i hardly have a clue as to what is going on, so can you guys please give me some ideas as to what tools to use to debug this? I have tried to see the redirect logs, but they really dont show much, and varnishlog isn't helping much either - although i must admit i am no professional at varnish.

I believe the problem is with varnish trying to cache the url, and thus apache redirects are not being done properly, however visits the site first has a redirect, and based on that other users are served the content, depending on from where the user was when the cache was last updated, is it correct? if so, how can i solve the problem?

Also, i have the option of using geoip redirects on varnish3 instead of using apache2 to do the redirects, is that what the best practice is? Any suggestion as to debugging this or to fix this would be helpful!


share|improve this question

There exist ways to use inline C in your VCL to have Varnish do your GeoIP detection. You need to do it there so that you redirect before the user gets served a cached page that might be intended for a different region.

share|improve this answer
that's the thing i was dreading.. i was just hoping to get some other workaround it - hopeful thinking doesn't get you far.. i am just worried about downtime, this can possibly lead to more downtime then small changes to apache configurations, i will keep this question open, see if i can get any other workaround, probably wont, but worth a try -- also, thanks! – mercy Jun 28 '12 at 16:47

I still don't have enough rights to write a comment so this is why I will post my answer here.

Based on the problem there is a big chance that varnish is the cause, especially if you use a setup like that:

varnish -> backend .com http server (which decides based on IP rules where to redirect the request) -> (.ru|.eu) web server.

So if that's the case varnish has no clue that the same address/request can have different content based on the IP address. So I guess once varnish cache the content will keep it there until it expires.

What I would recommend is to use varnishlog and do some testing emulating an EU customer and a RU one. If you don't have access to an outside IP from Russia just use some Amazon EC2 instances :)

Hopefully this will help.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.