LNMP 架构——Nginx 默认虚拟主机配置

修改 nginx 主配置文件

[root@dl-001 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
#  删除原有的server语句块,替换为下面的代码

include vhost/*.conf;

创建并修改虚拟主机配置文件(默认虚拟主机)

[root@dl-001 nginx-1.12.2]# cd /usr/local/nginx/conf
[root@dl-001 conf]# mkdir vhost
[root@dl-001 conf]# cd vhost/
[root@dl-001 vhost]# vim aaa.com.conf
server
{
    // 指定监听80端口,并将该虚拟主机设置为默认虚拟主机
    listen 80 default_server;
    
    // 设置服务器的名称
    server_name aaa.com;
    
    // 设置服务器默认网页
    index index.html index.htm index.php;
    
    // 设置服务器的根目录
    root /data/www/default;
}

创建默认虚拟主机的根目录及默认页面

[root@dl-001 vhost]# mkdir -p /data/www/default
[root@dl-001 vhost]# cd /data/www/default/

[root@dl-001 default]# vim index.html
aaa.com

检测代码并重启服务

[root@dl-001 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@dl-001 default]# /usr/local/nginx/sbin/nginx -s reload

检测是否成功

[root@dl-001 default]# curl -x 127.0.0.1:80 aaa.com
aaa.com

// 由于是默认的虚拟主机,任何域名都可以显示默认网页信息
[root@dl-001 default]# curl -x 127.0.0.1:80 bbb.com
aaa.com

[root@dl-001 default]# curl -x 127.0.0.1:80 ccc.com
aaa.com