nginx基于ip的虚拟主机实战

简介:

1    网卡eth0配置多个ip地址

一个网卡多个ip,如何一个网卡配置多个ip

方法1:ifconfig eth0:0 10.0.0.101/24 up    (ifconfig 方式设置ip叫做别名IP)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@web01 conf] # ifconfig eth0:0 10.0.0.101/24 up
[root@web01 conf] # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:79:73:42  
           inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe79:7342 /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:2694 errors:0 dropped:0 overruns:0 frame:0
           TX packets:1959 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:243597 (237.8 KiB)  TX bytes:229882 (224.4 KiB)
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:79:73:42  
           inet addr:10.0.0.101  Bcast:10.0.0.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:79:73:4C  
           inet addr:172.16.1.8  Bcast:172.16.1.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe79:734c /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
           TX packets:154 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:0 (0.0 b)  TX bytes:9398 (9.1 KiB)
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:65536  Metric:1
           RX packets:129 errors:0 dropped:0 overruns:0 frame:0
           TX packets:129 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0 
           RX bytes:11352 (11.0 KiB)  TX bytes:11352 (11.0 KiB)

在管理机m01上面ping是否可以ping通

1
2
3
4
5
6
7
8
9
[root@m01 ~] # ping 10.0.0.101 -c4
PING 10.0.0.101 (10.0.0.101) 56(84) bytes of data.
64 bytes from 10.0.0.101: icmp_seq=1 ttl=64  time =0.421 ms
64 bytes from 10.0.0.101: icmp_seq=2 ttl=64  time =0.416 ms
64 bytes from 10.0.0.101: icmp_seq=3 ttl=64  time =0.384 ms
64 bytes from 10.0.0.101: icmp_seq=4 ttl=64  time =0.397 ms
--- 10.0.0.101  ping  statistics ---
4 packets transmitted, 4 received, 0% packet loss,  time  3000ms
rtt min /avg/max/mdev  = 0.384 /0 .404 /0 .421 /0 .025 ms

方法2:    辅助IP设置ip addr add 10.0.0.102/24 dev eth0 label eth0:1(不带表情label ifconifg看不了)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@web01 conf] # ip addr add 10.0.0.102/24 dev eth0 label eth0:1
[root@web01 conf] # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:79:73:42  
           inet addr:10.0.0.8  Bcast:10.0.0.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe79:7342 /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:2920 errors:0 dropped:0 overruns:0 frame:0
           TX packets:2103 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:262933 (256.7 KiB)  TX bytes:247258 (241.4 KiB)
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:79:73:42  
           inet addr:10.0.0.101  Bcast:10.0.0.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:79:73:42  
           inet addr:10.0.0.102  Bcast:0.0.0.0  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:79:73:4C  
           inet addr:172.16.1.8  Bcast:172.16.1.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe79:734c /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:6 errors:0 dropped:0 overruns:0 frame:0
           TX packets:154 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:360 (360.0 b)  TX bytes:9398 (9.1 KiB)
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:65536  Metric:1
           RX packets:129 errors:0 dropped:0 overruns:0 frame:0
           TX packets:129 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0 
           RX bytes:11352 (11.0 KiB)  TX bytes:11352 (11.0 KiB)

        用ip add可以看出来ip的别名和辅助ip区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@web01 conf] # ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1 /8  scope host lo
     inet6 ::1 /128  scope host 
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:79:73:42 brd ff:ff:ff:ff:ff:ff
     inet 10.0.0.8 /24  brd 10.0.0.255 scope global eth0
     inet 10.0.0.101 /24  brd 10.0.0.255 scope global secondary eth0:0
     inet 10.0.0.102 /24  scope global secondary eth0:1
     inet6 fe80::20c:29ff:fe79:7342 /64  scope link 
        valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link /ether  00:0c:29:79:73:4c brd ff:ff:ff:ff:ff:ff
     inet 172.16.1.8 /24  brd 172.16.1.255 scope global eth1
     inet6 fe80::20c:29ff:fe79:734c /64  scope link 
        valid_lft forever preferred_lft forever

        到管理机m01查看辅助ip地址是否通

1
2
3
4
5
6
7
8
9
[root@m01 ~] # ping -c4 10.0.0.102
PING 10.0.0.102 (10.0.0.102) 56(84) bytes of data.
64 bytes from 10.0.0.102: icmp_seq=1 ttl=64  time =1.65 ms
64 bytes from 10.0.0.102: icmp_seq=2 ttl=64  time =0.330 ms
64 bytes from 10.0.0.102: icmp_seq=3 ttl=64  time =0.379 ms
64 bytes from 10.0.0.102: icmp_seq=4 ttl=64  time =0.318 ms
--- 10.0.0.102  ping  statistics ---
4 packets transmitted, 4 received, 0% packet loss,  time  3003ms
rtt min /avg/max/mdev  = 0.318 /0 .670 /1 .655 /0 .569 ms


2    修改nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@web01 conf] # cat 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       10.0.0.8:80;
         server_name  www.etiantian.org;
         location / {
             root   html /www ;
             index  index.html index.htm;
         }
     }
     server {
         listen       10.0.0.101:80;
         server_name  www.etiantian.org;
         location / {
             root   html /bbs ;
             index  index.html index.htm;
         }
     }
     server {
         listen       10.0.0.102:80;
         server_name  www.etiantian.org;
         location / {
             root   html /blog ;
             index  index.html index.htm;
         }
     }
}              
[root@web01 conf] # /application/nginx/sbin/nginx -t
nginx: the configuration  file  /application/nginx-1 .6.3 //conf/nginx .conf syntax is ok
nginx: configuration  file  /application/nginx-1 .6.3 //conf/nginx .conf  test  is successful
[root@web01 conf] # /application/nginx/sbin/nginx -s reload

    2.1    验证,在m01管理机上面验证

1
2
3
4
[root@m01 ~] # curl 10.0.0.8
www
[root@m01 ~] # curl 10.0.0.101
www

        发现101是错误的,原因:有时候平滑优雅重启不生效。在web01机器上面关闭nginx服务再开启nginx服务。

1
2
[root@web01 conf] # /application/nginx/sbin/nginx -s stop
[root@web01 conf] # /application/nginx/sbin/nginx

        继续在管理机m01上验证

1
2
3
4
5
6
[root@m01 ~] # curl 10.0.0.8
www
[root@m01 ~] # curl 10.0.0.101
bbs
[root@m01 ~] # curl 10.0.0.102
blog

    上面已验证成功。


总结:

配置ip    (ifconfig是配置别名ip地址,ip addr是配置辅助ip地址)

ifconfig eth0:0 10.0.0.101/24 up

ip addr add 10.0.0.102/24 dev eth0 label eth0:1

删除ip

ifconfig eth0:0 down

ip addr del 10.0.0.102/24 dev eth0 label eth0:1

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


sandshell

相关文章
|
19天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
24 0
|
2月前
|
应用服务中间件 PHP 开发工具
Nginx解析环境搭建及实战
Nginx解析环境搭建及实战
25 0
|
18天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
31 2
|
4月前
|
Java 应用服务中间件 nginx
Nginx之服务端获取真实客户端IP
Nginx之服务端获取真实客户端IP
138 5
|
11天前
|
域名解析 Ubuntu 应用服务中间件
Nginx实现虚拟主机
Nginx实现虚拟主机
|
5月前
|
搜索推荐 应用服务中间件 nginx
Nginx系列教程(05) - 虚拟主机配置
Nginx系列教程(05) - 虚拟主机配置
179 0
|
1月前
|
域名解析 网络协议 应用服务中间件
nginx-ingress通过ipv6暴露服务,并在nginx ingress日志中记录客户端真实ipv6的ip地址
本文主要通过阿里云提供的clb和nlb来实现,建议是提前创建好双栈的vpc和vsw(使用clb可以不用双栈vpc和vsw)
176 1
|
2月前
|
监控 安全 应用服务中间件
|
2月前
|
运维 应用服务中间件 Shell
Nginx安装与虚拟主机配置shell脚本
Nginx安装与虚拟主机配置shell脚本
27 0
|
2月前
|
网络协议 应用服务中间件 Apache
在 NGINX 中根据用户真实 IP 进行限制
在 NGINX 中根据用户真实 IP 进行限制