判断 HTTP 请求的 HOST 首部,如果不是 sundayhk.com 或者 sundayle.sundayhk.com , 则进行重定向到 sundayhk.com
通过设置变量到达条件组合的效果。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23server {
listen 80;
server_name sundayhk.com sundayle.sundayhk.com *.sundayhk.com;
root /www/sunayle.com;
set $myhost '';
if ($host = 'sundayhk.com') {
set $myhost 1;
}
if ($host = 'sundayle.sundayhk.com') {
set $myhost 1;
}
if ($myhost != 1) {
rewrite ^/(.*)$ http://sundayhk.com/$1 permanent;
}
location / {
try_files $uri $uri/ =404;
}
}
上面的组合是用于其他的。
如果是要跳转可以简单点1
2
3if ($host ! = 'sundayhk.com') {
return 301 http://sundayle.sundayhk.com;
}
更高效可以用两个server, 再return