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 have two domains and I want the user to be redirected to the closest server, but if they want to view the other server, they can by clicking a link in the site.

I tried to do it with a cookie, but it doesn't seem to be working. I think the problem is that the cookie is set for the refering URL and not the redirected URL, so nginx doesn't even see the cookie. How can I do this? Here is my attempt so far.

server {
    listen 80;

    set $redir "";
    if ($http_accept_language ~* "en") {
        set $redir "1";
    if ($http_cookie ~* "noredir") {
        set $redir "";
    if ($redir = "1") {
        rewrite ^;
share|improve this question
up vote 0 down vote accepted

As you said yourself, because cookies are domain dependent, the one set on .com will not be seen by So, in your redirect, you can add a parameter like ?noredir=true. Then when the .com receives that request, it can not redirect and set the cookie so it remembers.

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.