Nginx默认虚拟主机

Nginx默认虚拟主机

配置默认虚拟主机

定义一个默认虚拟主机有两种方法

1、放到一个目录里,根据名称来设置

2、在配置文件里设置default_server

[root@antong vhost]# echo "this is a default site." >> /data/nginx/default/index.html[root@antong nginx]# vim conf/nginx.conf  //将server内容部分删掉,末尾加入include vhost/*.conf;
···
application/xml;
include vhost/*.conf;
}
[root@antong nginx]# cd conf/
[root@antong conf]# mkdir vhost
[root@antong conf]# cd vhost/
[root@antong vhost]# vim aaa.com.conf
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/nginx/default;
}
[root@antong vhost]# mkdir -p /data/nginx/default
[root@antong vhost]# echo "this is a default site." >> /data/nginx/default/index.html
[root@antong vhost]# /usr/local/nginx/sbin/nginx -t               
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@antong vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@antong vhost]# curl localhost
this is a default site.

因为定义了默认虚拟主机,所以只要访问ip,不管域名是什么都会跳转到默认的虚拟主机

[root@antong vhost]# curl -x127.0.0.1:80 bbb.com
this is a default site.
[root@antong vhost]# curl -x127.0.0.1:80 aaa.com   
this is a default site.
posted @ 2021-09-06 11:37  殇黯瞳  阅读(163)  评论(0编辑  收藏  举报