判断 HTTP 请求的 HOST 首部,如果不是 sundayle.com 或者 www.sundayle.com , 则进行重定向到 sundayle.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 sundayle.com www.sundayle.com *.sundayle.com;
root /www/sunayle.com;
set $myhost '';
if ($host = 'sundayle.com') {
set $myhost 1;
}
if ($host = 'www.sundayle.com') {
set $myhost 1;
}
if ($myhost != 1) {
rewrite ^/(.*)$ http://sundayle.com/$1 permanent;
}
location / {
try_files $uri $uri/ =404;
}
}
上面的组合是用于其他的。
如果是要跳转可以简单点1
2
3if ($host ! = 'sundayle.com') {
return 301 http://www.sundayle.com;
}
更高效可以用两个server, 再return