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