设为首页 收藏本站
查看: 1066|回复: 0

[经验分享] Nginx 构建虚拟主机

[复制链接]

尚未签到

发表于 2018-11-10 12:25:04 | 显示全部楼层 |阅读模式
  虚拟主机
  虚拟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网站服务,参考http://blog.51cto.com/13760351/2160042
  实验目标
  1.搭建基于IP的虚拟主机
  2.搭建基于域名的虚拟主机
  3.搭建基于不同端口号的虚拟主机
  
  实验步骤
  
  
  一、搭建基于不同域名的虚拟主机
  1.相同IP 相同端口 不同主机名 benet 和 accp
  [root@localhost conf]# vim nginx.conf
  server {
  server_name  www.benet.com;    /监听地址
  location / {
  root   /var/www/benet;          /www.benet.com 的工作目录
  index  index.html index.php;
  }
  }
  server {
  server_name  www.accp.com;     /监听地址
  location / {
  root   /var/www/accp;         /www.accp.com 的工作目录
  index  index.html index.php;
  }
  }
  }                        /这个括号需要把原文件末尾的括号给去掉
  [root@localhost conf]# nginx –t
  [root@localhost conf]# service nginx.conf
  
  2.分别创建不同目录,并写入不同站点内容
  [root@localhost www]# mkdir –p /var/www/html/benet       /创建虚拟用户benet目录
  [root@localhost www]# mkdir –p /var/www/html/accp        /创建虚拟用户yun目录
  [root@localhost conf.d]# echo “this is benet” > /var/www/html/benet/index.html    /站点写入内容
  [root@localhost conf.d]# echo “this is accp” > /var/www/html/accp/index.html        /写入内容
3.搭建DNS服务,更多步骤详见http://blog.51cto.com/13760351/2158118  [root@localhost conf.d]# service iptables stop        /关闭防火墙
  [root@localhost conf.d]# setenforce 0                     /关闭安全性
  [root@localhost conf.d]# service samed start        /启动dns域名解析服务
4.在客户机上设置dns解析并访问2个域名
DSC0000.jpg

DSC0001.jpg

DSC0002.jpg

二、搭建基于端口的虚拟主机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 ok
  nginx: 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.客户端访问验证
DSC0003.jpg

DSC0004.jpg

  三、基于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/www/benet;
  index  index.html index.php;
  }
  }
  server {
  listen 192.168.100.200:80;
  server_name  192.168.100.200:80;
  location / {
  root   /var/www/accp;
  index  index.html index.php;
  }
  }
  }
  [root@localhost conf]# mkdir -p /var/www/benet  /创建www.benet.com根目录
  [root@localhost conf]# mkdir -p /var/www/test   /创建www.benet.com根目录
  [root@localhost conf]# echo "this is benet web " > /var/www/benet/index.html  /写入网页内容
  [root@localhost conf]# echo "this is test web " > /var/www/test/index.html    /写入网页内容
  [root@localhost conf]# 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@localhost conf]# service nginx stop
  [root@localhost conf]# service nginx start              /重启服务
  3.分别访问2个IP地址
DSC0005.jpg

DSC0006.jpg




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633240-1-1.html 上篇帖子: Centos7 下面安装docker 部署Nginx 下篇帖子: linux下安装配置nginx-Geyh的博客
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表