Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX rewrite url if specific domain and specific url

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.

like image 818
Filip Avatar asked Sep 11 '25 05:09

Filip


2 Answers

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
  }
}
like image 64
VBart Avatar answered Sep 13 '25 23:09

VBart


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;
    }
  }
}
like image 33
cnst Avatar answered Sep 13 '25 22:09

cnst