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

修改nginx主配置文件

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

include vhost/*.conf;
  • 1
  • 2
  • 3
  • 4

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

[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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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

[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
  • 1
  • 2
  • 3
  • 4
  • 5

检测代码并重启服务

[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
  • 1
  • 2
  • 3
  • 4

检测是否成功

[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

猜你喜欢

转载自blog.csdn.net/yongzhen150/article/details/80813635
今日推荐