查看当前网络接口IP信息
[root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:2F:3B:EA inet addr:192.168.244.134 Bcast:192.168.244.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2f:3bea/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:65028 errors:0 dropped:0 overruns:0 frame:0 TX packets:49997 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:73780559 (70.3 MiB) TX bytes:4130968 (3.9 MiB) Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@localhost ~]#
查看eth0配置文件信息
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# vi ifcfg-eth0 DEVICE="eth0" BOOTPROTO="static" HWADDR="00:0C:29:2F:3B:EA" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="8c8eb461-11b0-445f-b67b-8d78111c2589" IPADDR=192.168.244.134 PREFIX=24 GATEWAY=192.168.244.1 DNS1=202.96.128.86
添加第一IP绑定配置文件
[root@localhost network-scripts]# vi ifcfg-eth0:1 DEVICE="eth0:1" BOOTPROTO="static" ONBOOT="yes" TYPE="Ethernet" IPADDR=192.168.244.136 PREFIX=24 GATEWAY=192.168.244.1
添加第二个IP绑定配置文件
[root@localhost network-scripts]# vi ifcfg-eth0:2 DEVICE="eth0:1" BOOTPROTO="static" ONBOOT="yes" TYPE="Ethernet" IPADDR=192.168.244.136 PREFIX=24 GATEWAY=192.168.244.1
查看当前系统下所有网络接口信息
[root@localhost network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:2F:3B:EA inet addr:192.168.244.134 Bcast:192.168.244.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2f:3bea/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:65992 errors:0 dropped:0 overruns:0 frame:0 TX packets:50693 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:73868397 (70.4 MiB) TX bytes:4234444 (4.0 MiB) Interrupt:19 Base address:0x2000 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:2F:3B:EA inet addr:192.168.244.136 Bcast:192.168.244.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:19 Base address:0x2000 eth0:2 Link encap:Ethernet HWaddr 00:0C:29:2F:3B:EA inet addr:192.168.244.138 Bcast:192.168.244.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@localhost network-scripts]#
通过命令行进行IP绑定
[root@localhost network-scripts]# ifconfig eth0:3 192.168.244.140 netmask 255.255.255.0 [root@localhost network-scripts]# ifconfig eth0:3 eth0:3 Link encap:Ethernet HWaddr 00:0C:29:2F:3B:EA inet addr:192.168.244.140 Bcast:192.168.244.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:19 Base address:0x2000 [root@localhost network-scripts]#
基于IP地址的虚拟主机配置
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir ../html/ipv1
[root@localhost conf]# touch ../html/ipv1/index.html
[root@localhost conf]# echo “IP based virtual host 1 OK”>../html/ipv1/index.html
[root@localhost conf]# mkdir ../html/ipv2
[root@localhost conf]# touch ../html/ipv2/index.html
[root@localhost conf]# echo “IP based virtual host 2 OK”>../html/ipv2/index.html
在nginx配置文件中添加基于IP的虚拟机主机配置
[root@localhost conf]# vi nginx.conf
server {
listen 192.168.244.136:80;
server_name 192.168.244.136;
access_log logs/ipv1_access.log;
location / {
root html/ipv1;
index index.html index.htm;
}
}
server {
listen 192.168.244.138:80;
server_name 192.168.244.138;
access_log logs/ipv2_access.log;
location / {
root html/ipv2;
index index.html index.htm;
}
}
确认修改后的配置文件语法正确性
[root@localhost conf]# nginx -t -c /usr/local/nginx/conf/nginx.conf
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
[root@localhost conf]#
基于主机名的虚拟机主机配置
建立测试目录及文件
[root@localhost conf]# mkdir ../html/nv1
[root@localhost conf]# touch ../html/nv1/index.html
[root@localhost conf]# echo “Name based virtual host 1 OK”>../html/nv1/index.html
[root@localhost conf]# mkdir ../html/nv2
[root@localhost conf]# touch ../html/nv2/index.html
[root@localhost conf]# echo “Name based virtual host 2 OK”>../html/nv2/index.html
[root@localhost conf]# mkdir ../html/nv3
[root@localhost conf]# touch ../html/nv3/index.html
[root@localhost conf]# echo “Name based virtual host 3 OK”>../html/nv3/index.html
在nginx配置文件中添加基于主机名的虚拟机主机配置
server {
listen 80;
server_name nv1.linuxcache.net;
access_log logs/nv1_access.log;
location / {
root html/nv1;
index index.html index.htm;
}
}
server {
listen 80;
server_name nv2.linuxcache.net nv3.linuxcache.net;
access_log logs/nv2_access.log;
location / {
root html/nv2;
index index.html index.htm;
}
}
server {
listen 80;
server_name *.linuxcache.cn;
access_log logs/nv3_access.log;
location / {
root html/nv3;
index index.html index.htm;
}
}
修改Windows客户机主机文件将测试域名指向虚拟机IP地址
C:\Windows\System32\drivers\etc\hosts
192.168.244.134 nv1.linuxcache.net
192.168.244.134 nv2.linuxcache.net
192.168.244.134 nv3.linuxcache.net
192.168.244.134 www.linuxcache.cn
192.168.244.134 mail.linuxcache.cn