nginx下的一级域名跳转到二级域名的配置

今天同事反馈,阿里打电话说“网站要变更不然今天就关停。”

这个事情去年年底就说了。不知道是什么原因。我们来解决这个问题。下面以testweb.com域名为例来讲讲。

阿里短信内容如下:

testweb.com域名网站内容与备案信息不符,请修改网站内容,保持与备案主体一致,符合最新备案规则。testweb.com网站下方未显示正确备案编号或者编号未指向工信部,请尽快添加,可参考操作指南。https://help.aliyun.com/knowledge_detail/137270.html

也是巧了,同事把这个短信发到我的微信,我就点开那个域名,确实是跳转到了我们公司的另外一个网站去了。这就很明显了,我跟同事说不要着急,这个问题简单,它不是备案问题,技术上可以轻松处理。做个域名跳转就OK,就是网上说的服务器做301跳转,301重定向,域名重定向。

那么nginx下的一级域名跳转到二级域名的配置,要怎么操作呢?我们分两步搞定。

第一步进入nginx虚拟主机配置目录,编辑相应站点的配置文件
[root@xxxxxxxxxxxxxxxxxxxxxxx sbin]# cd /usr/local/nginx/conf/vhost/
[root@xxxxxxxxxxxxxxxxxxxxxxx vhost]# vi testweb.conf

在server里面添加如下代码即可:

if ($host = 'testweb.com' ) {
                rewrite ^/(.*)$ http://www.testweb.com/$1 permanent;
}

完整的如下

server
    {
        listen 80;
        server_name  www.testweb.com testweb.com;
        index index.html index.htm index.php;
        root /data/www/testweb/public;

        location / {
          index  index.php index.html index.htm;
          #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
          if (!-e $request_filename)
          {
             #地址作为将参数rewrite到index.php上。
             rewrite ^/(.*)$ /index.php?s=$1;
             #若是子目录则使用下面这句,将subdir改成目录名称即可。
             #rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1;
          }
        }
        #这里就是nginx下的一级域名跳转到二级域名的配置,效果是访问testweb.com跳转到www.testweb.com
        if ($host = 'testweb.com' ) {
                rewrite ^/(.*)$ http://www.testweb.com/$1 permanent;
        }
        location /api/ {
                index  index.php index.html index.htm;
                #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
                if (!-e $request_filename)
                {
                #若是子目录则使用下面这句,将subdir改成目录名称即可。
                rewrite ^/api/(.*)$ /api.php?s=$1;
                 }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   unix:/usr/local/php/var/run/php-cgi.sock;
            fastcgi_index  index.php;
            #fastcgi_split_path_info ^(.+\.php)(.*)$;
            #fastcgi_param   PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|json|swg)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            #deny all;
        }

        access_log  /data/www/nginx_log/testweb.log access;
}

编辑完按esc,再按shift+:号,输入wq保存退出。

第二步重启nginx
[root@xxx vhost]# cd /usr/local/nginx/sbin/
[root@xxxxxxxxxxxxxxxxxxxxxxx sbin]# ./nginx -s reload

到此nginx下的一级域名跳转到二级域名的配置就搞定了。

刷新testweb.com看效果,可以看到已经跳转到了www.testweb.com。

完工。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值