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

相关文章
|
2月前
|
存储 应用服务中间件 nginx
在使用Nginx之后,如何在web应用中获取用户IP以及相关原理
但总的来说,通过理解网络通信的基础知识,了解http协议以及nginx的工作方式,我们已经能在大多数情况下准确地获取用户的真实IP地址了,在调试问题或者记录日志时会起到很大的帮助。
188 37
|
9月前
|
缓存 负载均衡 安全
Nginx常用基本配置总结:从入门到实战的全方位指南
Nginx常用基本配置总结:从入门到实战的全方位指南
863 0
|
7月前
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
632 3
|
8月前
|
应用服务中间件 网络安全 nginx
轻松上手Nginx Proxy Manager:安装、配置与实战
Nginx Proxy Manager (NPM) 是一款基于 Nginx 的反向代理管理工具,提供直观的 Web 界面,方便用户配置和管理反向代理、SSL 证书等。本文档介绍了 NPM 的安装步骤,包括 Docker 和 Docker Compose 的安装、Docker Compose 文件的创建与配置、启动服务、访问 Web 管理界面、基本使用方法以及如何申请和配置 SSL 证书,帮助用户快速上手 NPM。
4575 1
|
11月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
11月前
|
应用服务中间件 持续交付 nginx
[nginx]借助nginx实现自动获取本机IP
[nginx]借助nginx实现自动获取本机IP
195 5
|
11月前
|
应用服务中间件 nginx
Nginx——虚拟主机简介
Nginx——虚拟主机简介
155 0
|
11月前
|
应用服务中间件 nginx 数据安全/隐私保护
nginx配置源IP访问控制
nginx配置源IP访问控制
141 0
|
搜索推荐 架构师 应用服务中间件
Nginx极简入门(三)基于端口的虚拟主机配置
前面讲了如何配置基于IP的虚拟主机,今天讲一讲Nginx如何基于端口的虚拟主机。 需要说明的是:由于本文章是nginx系列文章中的一篇,文章里面很多其他的配置,可能前面的文章已经说讲过,然后后续就没有在介绍,如果出现有些配置没有讲,大家可能需要去看看前面的文章。
Nginx极简入门(三)基于端口的虚拟主机配置
|
搜索推荐 应用服务中间件 nginx
Nginx系列教程(05) - 虚拟主机配置
Nginx系列教程(05) - 虚拟主机配置
866 0