如果特定域和特定网址,NGINX会重写网址

时间:2013-11-15 11:37:58

标签: nginx rewrite

当用户使用root(aka /)url访问特定域时,我需要重写url。 到目前为止,我有:

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
  }
}

如何仅为NAME2.COM重写。有时NGINX语法会使我的堆栈溢出。请帮忙。

2 个答案:

答案 0 :(得分:2)

您应该拆分服务器块。请参阅: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
  }
}

答案 1 :(得分:1)

您使用if,如下所示:

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

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