Nginx上虚拟主机的实现过程

来源:互联网 发布:刷开团提醒软件 编辑:程序博客网 时间:2024/05/22 14:02


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

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

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

[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.88181.com" >sites/www/index.html //创建测试主页 
[root@rhel6u3-7 nginx]# echo "This is www1.88181.com" >sites/www1/index.html //创建测试主页 
[root@rhel6u3-7 nginx]# chown nginx. server/ sites/ -R //设置目前的属主和属组为nginx

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

[root@rhel6u3-7 nginx]# vim conf/nginx.conf 
……… //在http模块中添加server字段,其次在server字段中添加location字段即可 
server { 
 listen 80; //设置虚拟主机监听端口为80 
 server_name www.88181.com; //设置虚拟主机域名 
 
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.88181.com; //设置include语句指向www1站点server字段配置文件位置 
…….. 

三、编辑网站www1.88181.com的server配置文件

[root@rhel6u3-7 ~]# cd /usr/local/nginx/server/ 
[root@rhel6u3-7 server]# vim www1.88181.com 
server { 
 listen 80; 
 server_name www1.88181.com; 
 
 location / { 
 root sites/www1; 
 index index.html index.htm; 
 } 
 location /status { // 查看nginx当前的状态情况,需要模块 “--with-http_stub_status_module”支持 
stub_status on; 
 access_log /usr/local/nginx/logs/www1_status.log; 
 auth_basic "NginxStatus"; } 
 }

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

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

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

[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

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

通过IE浏览器访问

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


linux
0 0
原创粉丝点击