利用虚拟主机技术,可以把一台真正的主机分成许多“虚拟”的主机,每一台虚拟主机都具有独立的域名和IP地址,具有完整的Internet服务器(www,FTP,email)功能。虚拟主机之间完全独立,在外界看来,每一台虚拟主机和一台独立的主机完全一样。效果一样但费用却大不一样了。由于多台虚拟主机共享一台真实主机的资源,每个虚拟主机用户承受的硬件费用、网络维护费用、通信线路的费用均大幅度降低,Internet真正成为人人用得起的网络!
虚拟主机共分为三种:基于IP的虚拟主机,基于端口的虚拟主机和基于名称的虚拟主机。前两种由于受到成本和客户使用习惯的限制,相对使用的没有基于名称的虚拟主机多,以下我们介绍一下三种虚拟主机的配置。
1. 基于名称的虚拟主机
首先,创建虚拟主机的目录,
1
2
3
|
[root@master nginx]
# mkdir -p /usr/local/nginx/virtual_sites/www.example.com
[root@master nginx]
# cd /usr/local/nginx/virtual_sites/
[root@master www.example.com]
# echo "Welcome to > index.html
|
修改nginx的配置文件,添加server字段,
1
2
3
4
5
6
7
|
[root@master nginx]# grep -B2 -A2 www.example.com conf/nginx.conf
server {
listen 80;
server_name www.example.com;
root virtual_sites/www.example.com;
index index.html index.htm;
}
|
之后,重启nginx服务,
1
2
3
4
5
6
|
[root@master nginx]
# service 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@master nginx]
#
|
客户端访问该虚拟主机,这里并没有配置DNS,使用了/etc/hosts作为域名解析,
1
|
[root@slave ~]
# links www.example.com
|
2. 基于IP的虚拟主机
1
2
3
4
5
|
server {
listen 192.168.1.150:80;
root virtual_sites/150.com;
index index.html index.htm;
}
|
添加虚拟IP地址,并重启nginx,
1
2
|
[root@master nginx]
# ifconfig eth1:0 192.168.1.150
[root@master nginx]
# service nginx restart
|
使用netstat查看,nginx是否启动成功,
1
2
3
4
5
6
7
8
9
10
11
|
[root@master nginx]
# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 192.168.1.151:8080 0.0.0.0:* LISTEN 1501
/nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1501
/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 801
/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 877
/master
tcp 0 0 192.168.1.129:22 192.168.1.106:59326 ESTABLISHED 1191
/sshd
tcp 0 0 :::22 :::* LISTEN 801
/sshd
tcp 0 0 ::1:25 :::* LISTEN 877
/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 1007
/dhclient
|
客户端来验证一把,
1
|
[root@slave ~]
# links 192.168.1.150
|
3. 基于端口的虚拟主机
1
2
3
4
5
|
server {
listen 8080;
root virtual_sites/151.com;
index index.html index.htm;
}
|
配置起来很简单,配置完毕,重启nginx服务,
1
2
3
4
5
|
[root@master nginx]
# service 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:
|
查看端口是否已监听8080,
1
2
3
4
5
6
7
8
9
10
11
|
[root@master nginx]
# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1526
/nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1526
/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 801
/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 877
/master
tcp 0 0 192.168.1.129:22 192.168.1.106:59326 ESTABLISHED 1191
/sshd
tcp 0 0 :::22 :::* LISTEN 801
/sshd
tcp 0 0 ::1:25 :::* LISTEN 877
/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 1007
/dhclient
|
客户端进行访问验证,
1
|
[root@slave ~]
# links 192.168.1.151:8080
|
版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任