Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have two domains www.domain.com and www.domain.com.tw. 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;
    server_name www.domain.com.tw;

    set $redir "";
    if ($http_accept_language ~* "en") {
        set $redir "1";
    if ($http_cookie ~* "noredir") {
        set $redir "";
    if ($redir = "1") {
        rewrite ^ https://www.domain.com/;
share|improve this question

1 Answer 1

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 .com.tw. 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.