Nginx自动跳转到带www域名规则配置、Nginx多域名向主域名跳转

nginx 域名跳转 Nginx跳转自动到www域名规则配置,如果设置使 xxx.com 域名在用户访问的时候自动跳转到 www.xxx.com 呢?

一、在你的域名管理里面定义 xxx.com 和 www.xxx.com 指向你的主机ip地址,可以使用 nslookup 命令测试

直接输入 nslookup xxx.com 和 nslookup www.xxx.com 都有指向ip的A记录即可。

二、在nginx里面配置 rewrite 规则。打开 Nginx.conf 文件找到server配置段:【以下是我的server配置段】

#############禁止IP地址访问###########
server{
  listen 80 default_server;
  server_name _;
  return 403;
}
#############禁止IP地址访问###########

server {
  listen 80;
  server_name www.xxx.com xxx.com;
  if ($host != 'www.xxx.com') {
  rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
  }
}

这样就是用户直接访问 xxx.com 直接跳转的www.xxx.com。即让不带 www 的域名跳转到带 www 的域名。

三、拓展

可以是多个二级域名、三级域名都可以随意跳转、或者让它们都跳转到 xxx.xxx.com 这个域名,添加如下语句即可

server {
  listen 80;
  server_name xxx.xxx.com xxx.xxx.org;
  if ($host != 'xxx.xxx.org') {
  rewrite ^/(.*)$ http://xxx.xxx.com/$1 permanent;
  }
}

上面这个可以让另外一个二级域名 xxx.xxx.org 跳转到 xxx.xxx.com

这样不至于放弃二级域名后,对搜索引擎造成影响。对seo很有帮助

更多nginx规则,欢迎大家一起学习!相互提高!

原文地址:https://www.cnblogs.com/meiling12/p/11654391.html