> Linux服务器 > nginx >

Nginx实战之Nginx上虚拟主机的实现过程

 利用虚拟主机技术,可以把一台真正的主机分成许多"虚拟"的主机,每一台虚拟主机都具有独立的域名和IP地址,具有完整的Internet服务器(www, FTP,email)功能。虚拟主机之间完全独立,在外界看来,每一台虚拟主机和一台独立的主机完全一样。效果一样但费用却大不一样了。由于多台虚拟主机共享一台真实主机的资源,每个虚拟主机用户承受的硬件费用、网络维护费用、通信线路的费用均大幅度降低,Internet真正成为人人用得起的网络!

目前生产环境中,大多数服务提供商都采用了虚拟主机的方式为客户提供web服务,虚拟主机包括基于IP的虚拟主机,基于端口的虚拟主机和基于名称的虚拟主机,由于目前最流行的是基于名称的虚拟主机,也就是可以通过相同端口、相同IP对应多个域名站点,本实验以这种方式为主进行讲解。

一、创建站点目录,主页、权限


  1. [root@rhel6u3-7 ~]# uname -r  //查看系统内核版本号 2.6.32-279.el6.i686 [root@rhel6u3-7 ~]# cat /etc/redhat-release //查看系统版本号 Red Hat Enterprise Linux Server release 6.3 (Santiago) [root@rhel6u3-7 ~]#

  2. [root@rhel6u3-7 nginx]# pwd

  3. /usr/local/nginx

  4. [root@rhel6u3-7 nginx]# mkdir server  sites  //创建server字段配置文件目录为server,站点主目录为sites

  5. [root@rhel6u3-7 nginx]# mkdir sites/www  sites/www1  //在站点主目录中创建子站点目录

  6. [root@rhel6u3-7 nginx]# echo "This is www.rsyslog.org" >sites/www/index.html  //创建测试主页

  7. [root@rhel6u3-7 nginx]# echo "This is www1.rsyslog.org" >sites/www1/index.html //创建测试主页

  8. [root@rhel6u3-7 nginx]# chown nginx. server/  sites/  -R //设置目前的属主和属组为nginx

二、编辑nginx主配置文件,并添加server字段以及location字段,两种方式添加。设置www.rsyslog.orgwww1.rsyslog.org 两台虚拟主机


  1. [root@rhel6u3-7 nginx]# vim conf/nginx.conf

  2. ……… //在http模块中添加server字段,其次在server字段中添加location字段即可

  3.    server {

  4.        listen       80;   //设置虚拟主机监听端口为80

  5.        server_name  www.rsyslog.org;  //设置虚拟主机域名

  6.  

  7.        location / {

  8.            root   sites/www;  //设置虚拟主机主目录相对路径

  9.            index  index.html index.htm; //设虚拟主机默认主页

  10.        }

  11. location /status {  // 查看nginx当前的状态情况,需要模块 “--with-http_stub_status_module”支持

  12.                stub_status on;

  13.                access_log /usr/local/nginx/logs/www_status.log; //设置日志存放位置并命名

  14.                auth_basic "NginxStatus"; }

  15.  

  16. }

  17. include /usr/local/nginx/server/www1.rsyslog.org;  //设置include语句指向www1站点server字段配置文件位置

  18. ……..

三、编辑网站www1.rsyslog.orgserver配置文件


  1. [root@rhel6u3-7 ~]# cd /usr/local/nginx/server/

  2. [root@rhel6u3-7 server]# vim www1.rsyslog.org

  3. server {

  4.     listen       80;

  5.     server_name  www1.rsyslog.org;

  6.  

  7.      location / {

  8.            root   sites/www1;

  9.            index  index.html index.htm;

  10.        }

  11.     location /status {  // 查看nginx当前的状态情况,需要模块 “--with-http_stub_status_module”支持

  12.                stub_status on;

  13.                access_log /usr/local/nginx/logs/www1_status.log;

  14.                auth_basic "NginxStatus"; }

  15.    }

四,在DNS的区域文件中添加两条A记录指向网站主机名

有关DNS搭建及配置请参看http://dreamfire.blog.51cto.com/418026/1091943


  1. //在DNS的区域文件中添加两个网站的A记录

  2. www             A       192.168.100.107

  3. www1            A       192.168.100.107

 

五、启动nginx服务,为了方便测试关闭防火墙并将selinux设置为premissive模式

 


  1. [root@rhel6u3-7 server]# /etc/rc.d/init.d/nginx restart

  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  4. Stopping nginx:                                           [  OK  ]

  5. Starting nginx:                                            [  OK  ]

  6. [root@rhel6u3-2 ~]# /etc/rc.d/init.d/iptables stop

  7. [root@rhel6u3-2 ~]# setenforce 0

 

六、通过windows系统测试,首先将网卡DNS设置为192.168.100.102,然后通过nslookup命令解析是否成功。

 

 

通过IE浏览器访问

 

 

在网址后面加上status可以查看网站目前的运行状态

 

本文出自 “小诺的Linux开源技术博客” 博客,请务必保留此出处http://dreamfire.blog.51cto.com/418026/1141018

(责任编辑:IT)