根域名做301永久跳转(nginx)无www跳转www

  1. 域名服务商设置域名解析
    example.com的根域名解析到目标服务器ip地址(主机记录是@),www.example.com的域名解析到目标服务器ip地址(主机记录是www),如果你想设置泛解析匹配其他所有域名(主机记录是*)
  2. nginx新建example.com.conf的vhost文件,内容如下:
    vi example.com.conf
    server {
    listen 80;
    server_name example.com;
    rewrite ^(.*)$ http://www.example.com$1 permanent;
    }
    server {
    listen 443 ssl http2;
    server_name example.com;
    rewrite ^(.*)$ https://www.example.com$1 permanent;
    ssl_certificate certs/Nginx/1_www.example.com_bundle.crt;
    ssl_certificate_key certs/Nginx/2_www.example.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. 这样浏览器访问example.com,就会自动跳转www.example.com了

You may also like...

发表评论