方式一:两个域名分开写
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
server {
listen 80;
server_name www.example.org;
...
}
方式二:两个域名写一起
server {
listen 80;
server_name example.com www.example.com;
# 跳转到www
if ($host = 'example.com' ) {
rewrite ^(/.*)$ http://www.example.com$1 permanent;
}
}
或者
server {
listen 80;
server_name example.com www.example.com;
# 跳转到www
if ($host ~ '^example.com'){
return 301 https://www.example.com$request_uri;
}
}