nginx重定向非www到www

记录非www域名全部重定向到www,和www重定向到一级域名。

非www重定向到www

  • 编辑nginx网站配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

server {
listen 80;
server_name www.domain.com domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.domain.com;
...
}

www重定向到一级域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

server {
listen 80;
server_name www.domain.com domain.com;
return 301 https://domain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.domain.com;
return 301 https://domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
...
}