nginx禁止非sever_name指定域名访问

 禁止非sever_name指定域名访问,将其访问指向默认站点;

设置非server_name指定域名访问,将该访问重写到test.1com
server {
listen 80 default;
rewrite ^(.*) http://test1.com permanent;
}
server {
listen 80 ;
server_name test1.com;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name test2.com;
root html/test2.com;
index index.html;
}

修改hosts文件,设置test1/2/.com均指向该服务器192.168.0.15
192.168.0.15 test2.com
192.168.0.15 test1.com
192.168.0.15 test3.com

访问test1.com、test2.com可以正常访问
访问test3.com的时候就会自动跳到test1.com

这种方式就是设置一个默认server,当没有匹配的sever_name时,执行该server下的规则;
也可以将rewrite更换为return 500; 403;404;等返回状态。

 

相关文章

随着云计算和网络技术的发展,越来越多的数据需要在服务器之...
阿里云服务器Tomcat无法从外部访问一、环境阿里云Ubuntu 12....
购买一系列的东西其实就是花钱买块区域,服务器,域名,云解...
前言昨天买了域名,服务器,然后搭建了环境,然后想他通过默...
前言由于服务器centos6.8安装mysql一直出现不能连接问题,然后...
前言用 SSH客户端登录上以后,自己想要在本地连接服务器上的...