根域名做301跳轉的方法:
1.在域名服務商,將根域名解析到目標服務器ip地址。
2.在nginx新建xxx.com.conf的vhost文件,內容如下:
vi xxx.com.conf
server {
listen 80;
server_name xxx.com;
rewrite ^(.*)$ http://www.xxx.com$1 permanent;
}
server {
listen 443 ssl http2;
server_name xxx.com;
rewrite ^(.*)$ https://www.xxx.com$1 permanent;
ssl_certificate certs/Nginx/1_www.xxx.com_bundle.crt;
ssl_certificate_key certs/Nginx/2_www.xxx.com.key;#由于這兩個命令的默認值已經好幾次發生了改變,因此不建議顯性定義,除非有需要額外定義的值
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
charset utf-8;
access_log logs/localhost.access.log main;
error_log logs/localhost.error.log warn;
root html;
index index.php index.html index.htm;
location / {
……
}
}
3.這樣在瀏覽器訪問xxx.com,就會自動跳轉到www.xxx.com了。