4

I need to rewrite url when users access specific domain with root (aka /) url. So far I have:

server {
  listen 80;
  server_name name1.com name2.com;

  location = / {
    # Well, I need this only for NAME2.COM (it should not rewrite NAME1.COM)
    rewrite name2.com/users/sign_in
  }
}

How do rewrite only for NAME2.COM. Sometimes NGINX syntax makes my stack overflow. Please help.

2 Answers 2

3

You should split your server block. See: http://wiki.nginx.org/Pitfalls

server {
  listen 80;
  server_name name1.com;

  location = / {
    # no rewrite here
  }
}

server {
  listen 80;
  server_name name2.com;

  location = / {
    # your rewrite here
  }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You'd use if, like this:

server {
  listen 80;
  server_name name1.com name2.com;

  location = / {
    if ($host = name2.com) {
      rewrite ^ /users/sign_in last;
    }
  }
}

1 Comment

The reason this answer is beautiful because the only place you can use if for is this. So this answer wins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.