Nginx多域名虚拟主机

目标:实现多域名访问服务器
进入到nginx配置文件,修改nginx.conf配置文件
在这里插入图片描述
在#gzip on;下删除所有内容,删除完成后加入以下内容,这里只以a b 两个网站为例,如果还有c d 等网站部署方法也一样,只需按照这种格式复制粘贴就好。

server {
listen 80;
server_name www.a.com;

    #access_log  logs/host.access.log  main;

    location / {
        root   html/a;
        index  index.html index.htm;
    }
}

server {
listen 80;
server_name www.b.com;

    #access_log  logs/host.access.log  main;
    
    location / {
        root   html/b;
        index  index.html index.htm;
     }
    }

}
在这里插入图片描述
具体注释
在这里插入图片描述
添加完成后去创建 a b 两个发布目录,需在默认发布目录下创建/usr/local/nginx/html
在这里插入图片描述
进入a发布目录修改网页内容
[root@localhost html]# cd a
[root@localhost a]# vim index.html

在这里插入图片描述
进入b发布目录修改网页内容
root@localhost a]# cd …/
[root@localhost html]# cd b
[root@localhost b]# vim index.html
在这里插入图片描述
在这里插入图片描述

检查服务是否能正常启动

[root@localhost b]# /usr/local/nginx/sbin/nginx -t
在这里插入图片描述
启动服务并查看80端口是否被监听到
[root@localhost b]# /usr/local/nginx/sbin/nginx
[root@localhost b]# netstat -nel |grep 80
在这里插入图片描述
因为我们域名不是真正的注册域名,所以只能在本地电脑etc文件添加域名解析后才能访问
在这里插入图片描述
访问a网页
在这里插入图片描述
访问b网页

在这里插入图片描述

1、a b 发布目录需要创建在nginx默认的发布目录下面

原创文章 14 获赞 17 访问量 1668

猜你喜欢

转载自blog.csdn.net/wyyfpzy/article/details/106183607