天天看点

Nginx实战基础篇三 Nginx上虚拟主机的实现过程

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

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

<b>一、创建站点目录,主页、权限</b>

[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 ~]#

[root@rhel6u3-7 nginx]# pwd 

/usr/local/nginx 

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

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

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

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

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

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

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

    server { 

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

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

        location / { 

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

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

        } 

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

                stub_status on; 

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

                auth_basic "NginxStatus"; } 

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

…….. 

<b>三、编辑网站</b><b>www1.rsyslog.org</b><b>的</b><b>server</b><b>配置文件</b>

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

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

server { 

     listen       80; 

     server_name  www1.rsyslog.org; 

      location / { 

            root   sites/www1; 

            index  index.html index.htm; 

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

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

    } 

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

<b></b>

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

www             A       192.168.100.107 

www1            A       192.168.100.107 

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

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

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 

Stopping nginx:                                           [  OK  ] 

Starting nginx:                                            [  OK  ] 

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

[root@rhel6u3-2 ~]# setenforce 0 

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

<a href="http://blog.51cto.com/attachment/201302/222243553.png" target="_blank"></a>

<b>通过</b><b>IE</b><b>浏览器访问</b>

<a href="http://blog.51cto.com/attachment/201302/222259594.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201302/222317134.png" target="_blank"></a>

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

<a href="http://blog.51cto.com/attachment/201302/222336780.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201302/222351801.png" target="_blank"></a>

本文转自凌激冰51CTO博客,原文链接:http://blog.51cto.com/dreamfire/1141018,如需转载请自行联系原作者

继续阅读