LNMP-Nginx域名重定向

说明:服务器运行的网站有时候可能有多个域名存在,用户输入多个域名访问站点则显示是输入的域名,公司需要多个域名中设置一个主域名,不论用户访问哪个域名均跳转到固定的主域名。
 
一:编辑nginx虚拟配置文件
[root@ghs ~]# vim /usr/local/nginx/conf/vhosts/test.conf
server
{
    listen 80;
    server_name www.test.com www.ghs.com www.aaa.com;    
    index index.html index.htm index.php;
    root /data/www;
 
加入下面文段到配置文件
     if ($host != 'www.test.com' )
{
        rewrite  ^/(.*)$  http://www.test.com/$1  permanent;
 }
 
 
2:测试用www.aaa.com访问测试
[root@ghs ~]# curl -x127.0.0.1:80 www.aaa.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.2
Date: Wed, 21 Jun 2017 02:04:06 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.test.com/  成功跳转到test.com域名
 
2:用www.ghs.com访问测试
[root@ghs ~]# curl -x127.0.0.1:80 www.ghs.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.2
Date: Wed, 21 Jun 2017 02:04:06 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.test.com/  成功跳转到test.com域名
posted @ 2019-09-27 23:22  一颗小豆子  阅读(229)  评论(0编辑  收藏  举报