您的位置:首页 > 运维架构 > Nginx

基于域名的虚拟主机配置(Nginx在 Linux下配置)

2017-04-17 21:45 603 查看
利用工具绑定域名:



需求:

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容

两个域名是www.nginx1.com和www.nginx2.com

nginx服务器使用虚拟机192.168.1.121

准备环境:

创建192.168.1.121虚拟机,保证本地电脑和虚拟网络通畅。

在192.168.1.121上安装nginx。

通过host文件指定www.nginx1.com和www.nginx2.com对应192.168.1.121虚拟机:

修改window的hosts文件:(C:\Windows\System32\drivers\etc)



html目录创建:

在192.168.1.121上创建/usr/local/nginx1_html,此目录为www.nginx1.com域名访问的目录

在192.168.1.121上创建/usr/local/nginx2_html,此目录为www.nginx2.com域名访问的目录

目录中的内容使用nginx自带的html文件,将/usr/local/nginx/html中的内容拷贝分别拷贝到上边两个目录中,并且将nginx1_html目录中的index.html内容改为:“Welcome to nginx1!”

将nginx2_html目录中的index.html内容改为“Welcome to nginx2!”

配置虚拟主机:

修改/usr/local/nginx/conf/nginx.conf文件,添加两个虚拟主机,如下:

#配置虚拟主机aaa.test.com
server {
#监听的ip和端口,配置本机ip和端口
listen 192.168.1.121:80;
#虚拟主机名称是www.nginx1.com,请求域名www.nginx1.com的url将由此server配置解析
server_name www.nginx1.com;
#所有的请求都以/开始,所有的请求都可以匹配此location
location / {
#使用root指令指定虚拟主机目录即网页存放目录
#比如访问http://ip/test.html将找到/usr/local/aaa_html/test.html
#比如访问http://ip/item/test.html将找到/usr/local/aaa_html/item/test.html
root /usr/local/nginx1_html;
#指定欢迎页面,按从左到右顺序查找
index index.html index.htm;
}
}

#配置虚拟主机bbb.test.com
server {
listen 192.168.101.3:80;
server_name www.nginx2.com;
location / {
root /usr/local/nginx2_html;
index index.html index.htm;
}
}


完成:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息