Nginx 构建虚拟主机

网友投稿 255 2022-10-14

Nginx 构建虚拟主机

虚拟主机

虚拟Web主机指的是在同一台服务器上运行多个Web站点,其中每一个站点实际上并不独自占用整个服务器,因此称为“虚拟”web主机。通过虚拟Web主机服务可以充分利用服务器硬件资源,从而降低网站构建及运行成本。同Apache一样,nginx也配置了3种类型虚拟主机。

Nginx虚拟Web主机分为三种■ 基于域名:每个虚拟机使用不同域名,相同IP■ 基于IP:每个虚拟机使用不同域名,且对应不同的IP■ 基于不同端口号:相同IP,不同TCP端口

备注:1.工作常用到的是基于不同域名的虚拟主机,其他两种虚拟主机运行并不是很多

2.在构建Apache虚拟主机时,省略了基于ip的虚拟主机实验,在这里特地给大家补上!

实验环境

Linux6.5系统

IP地址:192.168.100.10

客户端IP地址:192.168.100.22

yum挂载目录:/mnt/sr0

已搭建Nginx网站服务,参考相同端口 不同主机名 benet 和 accp

[root@localhost conf]# vim nginx.conf

server {        server_name  benet.com;    /监听地址        location / {            root   /var//benet.com 的工作目录            index  index.html index.php;        }    }    server {        server_name  accp.com;     /监听地址        location / {            root   /var//accp.com 的工作目录            index  index.html index.php;        }    }}                        /这个括号需要把原文件末尾的括号给去掉

[root@localhost conf]# nginx –t

[root@localhost conf]# service nginx.conf

2.分别创建不同目录,并写入不同站点内容

[root@localhost mkdir –p /var//创建虚拟用户benet目录[root@localhost mkdir –p /var//创建虚拟用户yun目录

[root@localhost conf.d]# echo “this is benet” > /var//站点写入内容

[root@localhost conf.d]# echo “this is accp” > /var//写入内容

3.搭建DNS服务,更多步骤详见conf.d]# service iptables stop        /关闭防火墙[root@localhost conf.d]# setenforce 0                    /关闭安全性

[root@localhost conf.d]# service samed start        /启动dns域名解析服务

4.在客户机上设置dns解析并访问2个域名

二、搭建基于端口的虚拟主机

1.修改nginx配置

server {        listen 192.168.100.10:6666;                       /监听6666端口        server_name  192.168.100.10:6666;

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

error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }

}

server {        listen 192.168.100.10:8888;                    /监听8888端口        server_name  192.168.100.10:8888;}

}

[root@localhost conf]# nginx –t                     /语法检查nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost conf]# service nginx stop[root@localhost conf]# service nginx start              /重启服务[root@localhost conf]# netstat -anpt | grep nginx          /查看端口状态tcp        0      0 192.168.100.10:8888         0.0.0.0:*               LISTEN      3689/nginx      /8888端口开启tcp        0      0 192.168.100.10:6666         0.0.0.0:*               LISTEN      3689/nginx       /6666端口开启

2.重启服务

[root@localhost conf]# service nginx.conf restart       /重启服务

3.客户端访问验证

三、基于IP的虚拟主机

1.添加双网卡,修改IP

eth0 :192.168.100.100

eth1 :192.168.100.200

2.修改配置文件

[root@localhost conf]# vim nginx.conf

server {         listen 192.168.100.100:80;        server_name  192.168.100.100:80;        location / {            root   /var/index  index.html index.php;        }    }    server {        listen 192.168.100.200:80;        server_name  192.168.100.200:80;         location / {            root   /var/index  index.html index.php;        }    }}

[root@localhost conf]# mkdir -p /var//创建benet.com根目录[root@localhost conf]# mkdir -p /var//创建benet.com根目录   [root@localhost conf]# echo "this is benet web " > /var//写入网页内容

[root@localhost conf]# echo "this is test web " > /var/ /写入网页内容

[root@localhost conf]# nginx –t                      /语法检查nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost conf]# service nginx stop[root@localhost conf]# service nginx start              /重启服务

3.分别访问2个IP地址

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:配置Azure负载均衡器对Web应用程序进行负载均衡
下一篇:Java this关键字的引用详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~