server { listen 80 default_server; // 有这个标记的就是默认虚拟主机 server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; }
[root@hanfeng conf]# vim /usr/local/nginx/conf/nginx.conf 删除的内容 server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
[root@hanfeng conf]# vim /usr/local/nginx/conf/nginx.conf 增长其中的一行 application/xml; include vhost/*.conf } 而后保存退出
[root@hanfeng conf]# mkdir /usr/local/nginx/conf/vhost [root@hanfeng conf]#
[root@hanfeng conf]# cd /usr/local/nginx/conf/vhost [root@hanfeng vhost]#
[root@hanfeng vhost]# vim aaa.com.conf 添加的文件内容 server { listen 80 default_server; server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; } 而后保存退出
[root@hanfeng vhost]# mkdir -p /data/wwwroot/default/ [root@hanfeng vhost]#
[root@hanfeng vhost]# cd /data/wwwroot/default/ [root@hanfeng default]#
[root@hanfeng default]# vim index.html 写入 This is the default site. 而后保存退出
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:47 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
在后面添加 ; 便可 include vhost/*.conf;
[root@hanfeng default]# /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@hanfeng default]# /usr/local/nginx/sbin/nginx -s reload [root@hanfeng default]#
[root@hanfeng default]# curl localhost This is the default site. [root@hanfeng default]# curl -x127.0.0.1:80 bbb.com This is the default site. [root@hanfeng default]#
由于修改了nginx.conf的配置,如今看到的默认索引页,是咱们刚刚新增的vhost的虚拟主机的索引页了 定义默认虚拟主机的两种办法: 1.默认虚拟主机,是根据目录的第一个.conf了进行选择,因此只须要在vhost目录下依次建立就能够了,固然这种方法不智能 2.只须要在vhost目录的.conf配置文件内,加上一个“default_server ”便可,把当前的这个配置对应的网站设置为第一个默认虚拟主机php