一,虚拟主机的概念:
虚拟主机是在网络服务器上划分出一定的磁盘空间供用户放置站点、应用组件等,提供必要的站点功能、数据存放和传输功能。所谓虚拟主机,也叫“网站空间”,就是把一台运行在互联网上的服务器划分成多个“虚拟”的服务器,每一个虚拟主机都具有独立的域名和完整的Internet服务器(支持WWW、FTP、E-mail等)功能,从用户角度来看,每台虚拟术机和一*立的服务器完全相同,在IP地址日益紧张的今天,基于域名的虚拟主机要比基于IP的虚拟主机使用的更加广泛。
二,配置基于IP的虚拟主机:
1,在eth0网卡上配置2个IP别名::192.168.0.41 和192.168.0.42,加入/etc/rc.local,让系统开机后运行:
vi /etc/rc.local
/sbin/ifconfig eth0:1 192.168.0.41 broadcast 192.168.0.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.0.41 dev eth0:1
/sbin/ifconfig eth0:2 192.168.0.42 broadcast 192.168.0.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.0.42 dev eth0:2
2,Nginx配置文件如下,配置两个IP地址支持静态文件的虚拟主机:
[root@www v1]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.0.41:80;
server_name 192.168.0.41;
location / {
index index.html index.htm;
root /usr/local/htdocs/v1;
#html网页存放目录
}
}
server {
listen 192.168.0.42:80;
server_name 192.168.0.42;
location / {
index index.html index.htm;
root /usr/local/htdocs/v2;
#html网页存放目录
}
}
}
3,测试结果:
三,配置基于域名的虚拟主机:
1,更改/etc/hosts文件,在文件末尾处添加以下两行:
192.168.0.5 www.tianyounb.com
192.168.0.5 tianyounb.com
[root@www ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 www.tianyounb.com www localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.0.5 www.tianyounb.com
192.168.0.5 tianyounb.com
2,Nginx配置文件如下,配置两个域名支持静态文件的虚拟主机:
[root@www v2]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.tianyounb.com;
location / {
index index.html index.htm;
root /usr/local/htdocs/v1;
#html网页存放目录
}
}
server {
listen 80;
server_name tianyounb.com;
location / {
index index.html index.htm;
root /usr/local/htdocs/v2;
#html网页存放目录
}
}
}
3,测试结果:
大家有更多的交流可以光临我的博客天游娱乐www.tianyounb.com,蓝冠在线www.languanbw.com谢谢!